From 38b4ecf2edbc4b1488def03b5f1eed6eb3d3cbf0 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 25 Oct 2019 11:03:13 +0000 Subject: [PATCH] Date/Time: Make sure `date_i18n()` correctly handles zero timestamp after [45901]. Props soulseekah, gravityview, Rarst. Reviewed by azaozz, SergeyBiryukov. Fixes #28636. git-svn-id: https://develop.svn.wordpress.org/trunk@46577 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 4 ++-- tests/phpunit/tests/date/dateI18n.php | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index f8255dd262..e7216f99ca 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -176,9 +176,9 @@ function date_i18n( $format, $timestamp_with_offset = false, $gmt = false ) { */ if ( 'U' === $format ) { $date = $timestamp; - } elseif ( $gmt && ! $timestamp_with_offset ) { // Current time in UTC. + } elseif ( $gmt && false === $timestamp_with_offset ) { // Current time in UTC. $date = wp_date( $format, null, new DateTimeZone( 'UTC' ) ); - } elseif ( ! $timestamp_with_offset ) { // Current time in site's timezone. + } elseif ( false === $timestamp_with_offset ) { // Current time in site's timezone. $date = wp_date( $format ); } else { /* diff --git a/tests/phpunit/tests/date/dateI18n.php b/tests/phpunit/tests/date/dateI18n.php index 5a13e3c437..84c43efc4c 100644 --- a/tests/phpunit/tests/date/dateI18n.php +++ b/tests/phpunit/tests/date/dateI18n.php @@ -19,6 +19,24 @@ class Tests_Date_I18n extends WP_UnitTestCase { $this->assertEquals( $wp_timestamp, date_i18n( 'U', 'invalid' ), '', 5 ); } + /** + * @ticket 28636 + */ + public function test_should_handle_zero_timestamp() { + $timezone = 'Europe/Kiev'; + update_option( 'timezone_string', $timezone ); + + $datetime = DateTimeImmutable::createFromFormat( + 'Y-m-d H:i:s', + '1970-01-01 00:00:00', + new DateTimeZone( $timezone ) + ); + $rfc3339 = $datetime->format( DATE_RFC3339 ); + + $this->assertEquals( 0, date_i18n( 'U', 0 ) ); + $this->assertEquals( $rfc3339, date_i18n( DATE_RFC3339, 0 ) ); + } + public function test_should_format_date() { $this->assertEquals( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( date_i18n( 'Y-m-d H:i:s' ) ), 'The dates should be equal', 2 ); }