Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WP 5.3: Use modern wp_timezone #13521

Merged
merged 4 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions _inc/lib/icalendar-reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,7 @@ function apply_timezone_offset( $events ) {
}

// get timezone offset from the timezone name.
$timezone_name = get_option( 'timezone_string' );
if ( $timezone_name ) {
$timezone = new DateTimeZone( $timezone_name );
$timezone_offset_interval = false;
} else {
// If the timezone isn't set then the GMT offset must be set.
// generate a DateInterval object from the timezone offset
$gmt_offset = get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
$timezone_offset_interval = date_interval_create_from_date_string( "{$gmt_offset} seconds" );
$timezone = new DateTimeZone( 'UTC' );
}
$timezone = wp_timezone();

$offsetted_events = array();

Expand All @@ -115,11 +105,6 @@ function apply_timezone_offset( $events ) {
$end_time = new DateTime( $end_time, $this->timezone );
$end_time->setTimeZone( $timezone );

if ( $timezone_offset_interval ) {
$start_time->add( $timezone_offset_interval );
$end_time->add( $timezone_offset_interval );
}

$event['DTSTART'] = $start_time->format( 'YmdHis\Z' );
$event['DTEND'] = $end_time->format( 'YmdHis\Z' );
}
Expand Down
34 changes: 11 additions & 23 deletions class.json-api-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -1533,47 +1533,35 @@ function format_date( $date_gmt, $date = null ) {
* relative to now and will convert it to local time using either the
* timezone set in the options table for the blog or the GMT offset.
*
* @param datetime string
* @param datetime string $date_string Date to parse.
*
* @return array( $local_time_string, $gmt_time_string )
*/
function parse_date( $date_string ) {
public function parse_date( $date_string ) {
$date_string_info = date_parse( $date_string );
if ( is_array( $date_string_info ) && 0 === $date_string_info['error_count'] ) {
// Check if it's already localized. Can't just check is_localtime because date_parse('oppossum') returns true; WTF, PHP.
if ( isset( $date_string_info['zone'] ) && true === $date_string_info['is_localtime'] ) {
$dt_local = clone $dt_utc = new DateTime( $date_string );
$dt_utc = new DateTime( $date_string );
$dt_local = clone $dt_utc;
$dt_utc->setTimezone( new DateTimeZone( 'UTC' ) );
return array(
(string) $dt_local->format( 'Y-m-d H:i:s' ),
(string) $dt_utc->format( 'Y-m-d H:i:s' ),
);
}

// It's parseable but no TZ info so assume UTC
$dt_local = clone $dt_utc = new DateTime( $date_string, new DateTimeZone( 'UTC' ) );
// It's parseable but no TZ info so assume UTC.
$dt_utc = new DateTime( $date_string, new DateTimeZone( 'UTC' ) );
$dt_local = clone $dt_utc;
} else {
// Could not parse time, use now in UTC
$dt_local = clone $dt_utc = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
// Could not parse time, use now in UTC.
$dt_utc = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
$dt_local = clone $dt_utc;
}

// First try to use timezone as it's daylight savings aware.
$timezone_string = get_option( 'timezone_string' );
if ( $timezone_string ) {
$tz = timezone_open( $timezone_string );
if ( $tz ) {
$dt_local->setTimezone( $tz );
return array(
(string) $dt_local->format( 'Y-m-d H:i:s' ),
(string) $dt_utc->format( 'Y-m-d H:i:s' ),
);
}
}
$dt_local->setTimezone( wp_timezone() );

// Fallback to GMT offset (in hours)
// NOTE: TZ of $dt_local is still UTC, we simply modified the timestamp with an offset.
$gmt_offset_seconds = intval( get_option( 'gmt_offset' ) * 3600 );
$dt_local->modify( "+{$gmt_offset_seconds} seconds" );
return array(
(string) $dt_local->format( 'Y-m-d H:i:s' ),
(string) $dt_utc->format( 'Y-m-d H:i:s' ),
Expand Down
32 changes: 32 additions & 0 deletions functions.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,38 @@
exit;
}

if ( ! function_exists( 'wp_timezone' ) ) {
/**
* Shim for WordPress 5.3's wp_timezone() function.
*
* This is a mix of wp_timezone(), which calls wp_timezone_string().
* We don't need both in Jetpack, so providing only one function.
*
* @since 7.9.0
* @todo Remove when WP 5.3 is Jetpack's minimum
*
* @return DateTimeZone Site's DateTimeZone
*/
function wp_timezone() {
$timezone_string = get_option( 'timezone_string' );

if ( $timezone_string ) {
return new DateTimeZone( $timezone_string );
}

$offset = (float) get_option( 'gmt_offset' );
$hours = (int) $offset;
$minutes = ( $offset - $hours );

$sign = ( $offset < 0 ) ? '-' : '+';
$abs_hour = abs( $hours );
$abs_mins = abs( $minutes * 60 );
$tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins );

return new DateTimeZone( $tz_offset );
}
}

/**
* Set the admin language, based on user language.
*
Expand Down
3 changes: 3 additions & 0 deletions packages/sync/src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ public static function roles() {
* 2. Check if `gmt_offset` is set, formats UTC-offset from it and return it.
* 3. Default to "UTC+0" if nothing is set.
*
* Note: This function is specifically not using wp_timezone() to keep consistency with
* the existing formatting of the timezone string.
*
* @return string
*/
public static function get_timezone() {
Expand Down