Skip to content

Commit

Permalink
use wp_timezone_string when available
Browse files Browse the repository at this point in the history
  • Loading branch information
rrennick committed May 29, 2020
1 parent fabebd9 commit 7fbe912
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
11 changes: 8 additions & 3 deletions includes/wc-formatting-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -713,20 +713,25 @@ function wc_string_to_datetime( $time_string ) {
* @return string PHP timezone string for the site
*/
function wc_timezone_string() {
// Added in WordPress 5.3 Ref https://developer.wordpress.org/reference/functions/wp_timezone_string/.
if ( function_exists( 'wp_timezone_string' ) ) {
return wp_timezone_string();
}

// If site timezone string exists, return it.
$timezone = get_option( 'timezone_string' );
if ( $timezone ) {
return $timezone;
}

// Get UTC offset, if it isn't set then return UTC.
$utc_offset = intval( get_option( 'gmt_offset', 0 ) );
if ( 0 === $utc_offset ) {
$utc_offset = floatval( get_option( 'gmt_offset', 0 ) );
if ( ! is_numeric( $utc_offset ) || 0.0 === $utc_offset ) {
return 'UTC';
}

// Adjust UTC offset from hours to seconds.
$utc_offset *= 3600;
$utc_offset = (int) ( $utc_offset * 3600 );

// Attempt to guess the timezone string from the UTC offset.
$timezone = timezone_name_from_abbr( '', $utc_offset );
Expand Down
6 changes: 3 additions & 3 deletions tests/legacy/unit-tests/formatting/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -699,15 +699,15 @@ public function test_wc_timezone_string() {

// Test with missing UTC offset.
delete_option( 'gmt_offset' );
$this->assertEquals( 'UTC', wc_timezone_string() );
$this->assertEquals( '+00:00', wc_timezone_string() );

// Test with manually set UTC offset.
update_option( 'gmt_offset', -4 );
$this->assertNotEquals( 'UTC', wc_timezone_string() );

// Test with invalid offset.
update_option( 'gmt_offset', 99 );
$this->assertEquals( 'UTC', wc_timezone_string() );
update_option( 'gmt_offset', 'invalid' );
$this->assertEquals( '+00:00', wc_timezone_string() );

// Restore default.
update_option( 'gmt_offset', '0' );
Expand Down

0 comments on commit 7fbe912

Please sign in to comment.