Date/Time: Introduce wp_timezone_string() and wp_timezone() for unified timezone retrieval.

* `wp_timezone_string()` retrieves the timezone from current settings as a string. Uses the `timezone_string` option to get a proper timezone if available, otherwise falls back to an offset.
* `wp_timezone()` retrieves the timezone from current settings as a `DateTimeZone` object. Timezone can be based on a PHP timezone string or a `±HH:MM` offset.

Props Rarst, remcotolsma, johnjamesjacoby, rmccue.
Fixes #24730.
Built from https://develop.svn.wordpress.org/trunk@45853


git-svn-id: http://core.svn.wordpress.org/trunk@45664 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2019-08-19 19:08:59 +00:00
parent 82af4ad1c0
commit 4896dc64ee
2 changed files with 44 additions and 2 deletions

View File

@ -74,7 +74,49 @@ function current_time( $type, $gmt = 0 ) {
}
/**
* Retrieve the date in localized format, based on a sum of Unix timestamp and
* Retrieves the timezone from current settings as a string.
*
* Uses the `timezone_string` option to get a proper timezone if available,
* otherwise falls back to an offset.
*
* @since 5.3.0
*
* @return string PHP timezone string or a ±HH:MM offset.
*/
function wp_timezone_string() {
$timezone_string = get_option( 'timezone_string' );
if ( $timezone_string ) {
return $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 $tz_offset;
}
/**
* Retrieves the timezone from current settings as a `DateTimeZone` object.
*
* Timezone can be based on a PHP timezone string or a ±HH:MM offset.
*
* @since 5.3.0
*
* @return DateTimeZone Timezone object.
*/
function wp_timezone() {
return new DateTimeZone( wp_timezone_string() );
}
/**
* Retrieves the date in localized format, based on a sum of Unix timestamp and
* timezone offset in seconds.
*
* If the locale specifies the locale month and weekday, then the locale will

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.3-alpha-45852';
$wp_version = '5.3-alpha-45853';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.