Date/Time: Rewrite and simplify get_gmt_from_date(), get_date_from_gmt(), and iso8601_to_datetime() using wp_timezone().

Improve unit test coverage.

Props Rarst, goodevilgenius.
Fixes #31809.

git-svn-id: https://develop.svn.wordpress.org/trunk@45887 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2019-08-23 11:02:51 +00:00
parent b975a251ff
commit ae2b4f9add
2 changed files with 147 additions and 66 deletions

View File

@@ -96,4 +96,114 @@ class Tests_Formatting_Date extends WP_UnitTestCase {
$gmt = gmdate( 'Y-m-d H:i:s' );
$this->assertEquals( strtotime( $gmt ), strtotime( get_gmt_from_date( $local ) ), 'The dates should be equal', 2 );
}
/**
* @ticket 31809
*
* @dataProvider timezone_provider
*/
public function test_gmt_from_date_correct_time( $timezone_string, $gmt_offset ) {
update_option( 'timezone_string', $timezone_string );
update_option( 'gmt_offset', $gmt_offset );
$local = new DateTimeImmutable( 'now', wp_timezone() );
$utc = $local->setTimezone( new DateTimeZone( 'UTC' ) );
$mysql_local = $local->format( 'Y-m-d H:i:s' );
$this->assertEquals( $utc->format( DATE_RFC3339 ), get_gmt_from_date( $mysql_local, DATE_RFC3339 ) );
}
/**
* @ticket 31809
*
* @dataProvider timezone_provider
*/
public function test_date_from_gmt_correct_time( $timezone_string, $gmt_offset ) {
update_option( 'timezone_string', $timezone_string );
update_option( 'gmt_offset', $gmt_offset );
$local = new DateTimeImmutable( 'now', wp_timezone() );
$utc = $local->setTimezone( new DateTimeZone( 'UTC' ) );
$mysql_utc = $utc->format( 'Y-m-d H:i:s' );
$this->assertEquals( $local->format( DATE_RFC3339 ), get_date_from_gmt( $mysql_utc, DATE_RFC3339 ) );
}
/**
* @ticket 31809
*
* @dataProvider timezone_provider
*/
public function test_is8601_to_datetime_correct_time( $timezone_string, $gmt_offset ) {
update_option( 'timezone_string', $timezone_string );
update_option( 'gmt_offset', $gmt_offset );
$format = 'Ymd\TH:i:sO';
$format_no_tz = 'Ymd\TH:i:s';
$local = new DateTimeImmutable( 'now', wp_timezone() );
$utc = $local->setTimezone( new DateTimeZone( 'UTC' ) );
$this->assertEquals(
$local->format( 'Y-m-d H:i:s' ),
iso8601_to_datetime( $local->format( $format ) ),
'Local time from local time.'
);
$this->assertEquals(
$utc->format( 'Y-m-d H:i:s' ),
iso8601_to_datetime( $local->format( $format ), 'gmt' ),
'UTC time from local time.'
);
$this->assertEquals(
$local->format( 'Y-m-d H:i:s' ),
iso8601_to_datetime( $local->format( $format_no_tz ) ),
'Local time from local time w/o timezone.'
);
$this->assertEquals(
$utc->format( 'Y-m-d H:i:s' ),
iso8601_to_datetime( $local->format( $format_no_tz ), 'gmt' ),
'UTC time from local time w/o timezone.'
);
$this->assertEquals(
$local->format( 'Y-m-d H:i:s' ),
iso8601_to_datetime( $utc->format( $format ) ),
'Local time from UTC time.'
);
$this->assertEquals(
$utc->format( 'Y-m-d H:i:s' ),
iso8601_to_datetime( $utc->format( $format ), 'gmt' ),
'UTC time from UTC time.'
);
$this->assertEquals(
$local->format( 'Y-m-d H:i:s' ),
iso8601_to_datetime( $utc->format( $format_no_tz ) . 'Z' ),
'Local time from UTC w/ Z timezone.'
);
$this->assertEquals(
$utc->format( 'Y-m-d H:i:s' ),
iso8601_to_datetime( $utc->format( $format_no_tz ) . 'Z', 'gmt' ),
'UTC time from UTC w/ Z timezone.'
);
}
/**
* Data provider to test different timezone modes.
*
* @return array
*/
public function timezone_provider() {
return array(
array(
'timezone_string' => 'Europe/Kiev',
'gmt_offset' => 3,
),
array(
'timezone_string' => '',
'gmt_offset' => 3,
),
);
}
}