Date/Time: Make sure current_time()
always calculates the offset from GMT/UTC.
Previously, UTC context was implied, but could be unreliable due to being affected by plugins. Props Rarst, rahulsprajapati, marco.marsala. Fixes #37440. git-svn-id: https://develop.svn.wordpress.org/trunk@43594 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
c1b7284c4b
commit
f1862cd98c
@ -69,7 +69,7 @@ function current_time( $type, $gmt = 0 ) {
|
|||||||
case 'timestamp':
|
case 'timestamp':
|
||||||
return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
|
return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
|
||||||
default:
|
default:
|
||||||
return ( $gmt ) ? date( $type ) : date( $type, time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
|
return ( $gmt ) ? gmdate( $type ) : gmdate( $type, time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
24
tests/phpunit/tests/date/currentTime.php
Normal file
24
tests/phpunit/tests/date/currentTime.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group date
|
||||||
|
* @group datetime
|
||||||
|
*/
|
||||||
|
class Tests_Date_CurrentTime extends WP_UnitTestCase {
|
||||||
|
|
||||||
|
public function test_should_work_with_changed_timezone() {
|
||||||
|
|
||||||
|
$format = 'Y-m-d H:i:s';
|
||||||
|
$timezone_string = 'America/Regina';
|
||||||
|
update_option( 'timezone_string', $timezone_string );
|
||||||
|
$datetime = new DateTime( 'now', new DateTimeZone( $timezone_string ) );
|
||||||
|
|
||||||
|
date_default_timezone_set( $timezone_string );
|
||||||
|
$this->assertEquals( gmdate( $format ), current_time( $format, true ) );
|
||||||
|
$this->assertEquals( $datetime->format( $format ), current_time( $format ) );
|
||||||
|
|
||||||
|
date_default_timezone_set( 'UTC' );
|
||||||
|
$this->assertEquals( gmdate( $format ), current_time( $format, true ) );
|
||||||
|
$this->assertEquals( $datetime->format( $format ), current_time( $format ) );
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user