Date/Time: Remove some legacy logic in `date_i18n()`.

Since there's no difference between using `date()` and `gmdate()` in WordPress, we can simply use the former in `date_i18n()` to reduce its complexity.

Adds tests.

Props jdgrimes for initial patch.
Fixes #37910.

git-svn-id: https://develop.svn.wordpress.org/trunk@38804 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler 2016-10-17 08:09:11 +00:00
parent 79fdcbdb07
commit b9894d3295
2 changed files with 89 additions and 14 deletions

View File

@ -91,13 +91,7 @@ function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {
$i = $unixtimestamp; $i = $unixtimestamp;
if ( false === $i ) { if ( false === $i ) {
if ( ! $gmt ) $i = current_time( 'timestamp', $gmt );
$i = current_time( 'timestamp' );
else
$i = time();
// we should not let date() interfere with our
// specially computed timestamp
$gmt = true;
} }
/* /*
@ -106,15 +100,13 @@ function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {
*/ */
$req_format = $dateformatstring; $req_format = $dateformatstring;
$datefunc = $gmt? 'gmdate' : 'date';
if ( ( !empty( $wp_locale->month ) ) && ( !empty( $wp_locale->weekday ) ) ) { if ( ( !empty( $wp_locale->month ) ) && ( !empty( $wp_locale->weekday ) ) ) {
$datemonth = $wp_locale->get_month( $datefunc( 'm', $i ) ); $datemonth = $wp_locale->get_month( date( 'm', $i ) );
$datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth ); $datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth );
$dateweekday = $wp_locale->get_weekday( $datefunc( 'w', $i ) ); $dateweekday = $wp_locale->get_weekday( date( 'w', $i ) );
$dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday ); $dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday );
$datemeridiem = $wp_locale->get_meridiem( $datefunc( 'a', $i ) ); $datemeridiem = $wp_locale->get_meridiem( date( 'a', $i ) );
$datemeridiem_capital = $wp_locale->get_meridiem( $datefunc( 'A', $i ) ); $datemeridiem_capital = $wp_locale->get_meridiem( date( 'A', $i ) );
$dateformatstring = ' '.$dateformatstring; $dateformatstring = ' '.$dateformatstring;
$dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring ); $dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring );
$dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring ); $dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring );
@ -142,7 +134,7 @@ function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {
} }
} }
} }
$j = @$datefunc( $dateformatstring, $i ); $j = @date( $dateformatstring, $i );
/** /**
* Filters the date formatted based on the locale. * Filters the date formatted based on the locale.

View File

@ -0,0 +1,83 @@
<?php
/**
* @group date
* @group datetime
*/
class Tests_Date_I18n extends WP_UnitTestCase {
public function test_should_format_date() {
$expected = date( 'Y-m-d H:i:s' );
$this->assertEquals( $expected, date_i18n( 'Y-m-d H:i:s' ) );
}
public function test_should_use_custom_timestamp() {
$expected = '2012-12-01 00:00:00';
$this->assertEquals( $expected, date_i18n( 'Y-m-d H:i:s', strtotime( '2012-12-01 00:00:00' ) ) );
}
public function test_date_should_be_in_gmt() {
$expected = date( 'Y-m-d H:i:s' );
$this->assertEquals( $expected, date_i18n( 'Y-m-d H:i:s', false, true ) );
}
public function test_custom_timestamp_ignores_gmt_setting() {
$expected = '2012-12-01 00:00:00';
$this->assertEquals( $expected, date_i18n( 'Y-m-d H:i:s', strtotime( '2012-12-01 00:00:00' ), false ) );
}
public function test_custom_timezone_setting() {
update_option( 'timezone_string', 'Europe/London' );
$expected = date( 'Y-m-d H:i:s', strtotime( date( 'Y-m-d H:i:s' ) ) + HOUR_IN_SECONDS );
$this->assertEquals( $expected, date_i18n( 'Y-m-d H:i:s' ) );
}
public function test_date_should_be_in_gmt_with_custom_timezone_setting() {
update_option( 'timezone_string', 'Europe/London' );
$expected = date( 'Y-m-d H:i:s' );
$this->assertNotEquals( date_i18n( 'Y-m-d H:i:s', false, false ), date_i18n( 'Y-m-d H:i:s', false, true ) );
$this->assertEquals( $expected, date_i18n( 'Y-m-d H:i:s', false, true ) );
}
public function test_date_should_be_in_gmt_with_custom_timezone_setting_and_timestamp() {
update_option( 'timezone_string', 'Europe/London' );
$expected = '2012-12-01 00:00:00';
$this->assertEquals( $expected, date_i18n( 'Y-m-d H:i:s', strtotime( '2012-12-01 00:00:00' ), false ) );
$this->assertEquals( $expected, date_i18n( 'Y-m-d H:i:s', strtotime( '2012-12-01 00:00:00' ), true ) );
}
public function test_adjusts_format_based_on_locale() {
$original_locale = $GLOBALS['wp_locale'];
/* @var WP_Locale $locale */
$locale = clone $GLOBALS['wp_locale'];
$locale->weekday[6] = 'Saturday_Translated';
$locale->weekday_abbrev[ 'Saturday_Translated' ] = 'Sat_Translated';
$locale->month[12] = 'December_Translated';
$locale->month_abbrev[ 'December_Translated' ] = 'Dec_Translated';
$locale->meridiem['am'] = 'am_Translated';
$locale->meridiem['AM'] = 'AM_Translated';
$GLOBALS['wp_locale'] = $locale;
$expected = 'Saturday_Translated (Sat_Translated) 01 December_Translated (Dec_Translated) 00:00:00 am_Translated AM_Translated';
$actual = date_i18n( 'l (D) d F (M) H:i:s a A', strtotime( '2012-12-01 00:00:00' ), false );
// Restore original locale.
$GLOBALS['wp_locale'] = $original_locale;
$this->assertEquals( $expected, $actual );
}
public function test_adjusts_format_based_on_timezone_string() {
update_option( 'timezone_string', 'Europe/Zurich' );
$this->assertEquals( '2012-12-01 00:00:00 CEST +02:00 Europe/Zurich', date_i18n( 'Y-m-d H:i:s T P e', strtotime( '2012-12-01 00:00:00' ), false ) );
}
}