From b9894d3295c40c8de5aba83178de65cb99916f57 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 17 Oct 2016 08:09:11 +0000 Subject: [PATCH] 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 --- src/wp-includes/functions.php | 20 ++----- tests/phpunit/tests/date/dateI18n.php | 83 +++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 14 deletions(-) create mode 100644 tests/phpunit/tests/date/dateI18n.php diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 292abf9355..a434ea5622 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -91,13 +91,7 @@ function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) { $i = $unixtimestamp; if ( false === $i ) { - if ( ! $gmt ) - $i = current_time( 'timestamp' ); - else - $i = time(); - // we should not let date() interfere with our - // specially computed timestamp - $gmt = true; + $i = current_time( 'timestamp', $gmt ); } /* @@ -106,15 +100,13 @@ function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) { */ $req_format = $dateformatstring; - $datefunc = $gmt? 'gmdate' : 'date'; - 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 ); - $dateweekday = $wp_locale->get_weekday( $datefunc( 'w', $i ) ); + $dateweekday = $wp_locale->get_weekday( date( 'w', $i ) ); $dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday ); - $datemeridiem = $wp_locale->get_meridiem( $datefunc( 'a', $i ) ); - $datemeridiem_capital = $wp_locale->get_meridiem( $datefunc( 'A', $i ) ); + $datemeridiem = $wp_locale->get_meridiem( date( 'a', $i ) ); + $datemeridiem_capital = $wp_locale->get_meridiem( date( 'A', $i ) ); $dateformatstring = ' '.$dateformatstring; $dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $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. diff --git a/tests/phpunit/tests/date/dateI18n.php b/tests/phpunit/tests/date/dateI18n.php new file mode 100644 index 0000000000..109e4d34d3 --- /dev/null +++ b/tests/phpunit/tests/date/dateI18n.php @@ -0,0 +1,83 @@ +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 ) ); + } +}