From 4896dc64eec4da606dea05f4c2309ca36cb1945f Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 19 Aug 2019 19:08:59 +0000 Subject: [PATCH] Date/Time: Introduce `wp_timezone_string()` and `wp_timezone()` for unified timezone retrieval. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * `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 --- wp-includes/functions.php | 44 ++++++++++++++++++++++++++++++++++++++- wp-includes/version.php | 2 +- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 019f8b6bf6..6a2fede355 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -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 diff --git a/wp-includes/version.php b/wp-includes/version.php index f594267057..ed2a327ac1 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.