From b1418a180ba1dd55fcc8c4633b6c3844471b5347 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 14 Feb 2024 16:56:43 +0900 Subject: [PATCH 1/2] fix: Time::createFromTimestamp() returns Time with UTC --- system/I18n/TimeTrait.php | 3 ++- tests/system/I18n/TimeTest.php | 14 +++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/system/I18n/TimeTrait.php b/system/I18n/TimeTrait.php index 93f03d8a84bd..3d70f8544fc6 100644 --- a/system/I18n/TimeTrait.php +++ b/system/I18n/TimeTrait.php @@ -266,7 +266,8 @@ public static function createFromFormat($format, $datetime, $timezone = null) public static function createFromTimestamp(int $timestamp, $timezone = null, ?string $locale = null) { $time = new self(gmdate('Y-m-d H:i:s', $timestamp), 'UTC', $locale); - $timezone ??= 'UTC'; + + $timezone ??= date_default_timezone_get(); return $time->setTimezone($timezone); } diff --git a/tests/system/I18n/TimeTest.php b/tests/system/I18n/TimeTest.php index ac2eb4facd61..d60fb4410a03 100644 --- a/tests/system/I18n/TimeTest.php +++ b/tests/system/I18n/TimeTest.php @@ -259,17 +259,21 @@ public function testCreateFromFormatWithInvalidFormat(): void public function testCreateFromTimestamp(): void { - // Set the timezone temporarily to UTC to make sure the test timestamp is correct + // Save the current timezone. $tz = date_default_timezone_get(); - date_default_timezone_set('UTC'); - $timestamp = strtotime('2017-03-18 midnight'); + // Change the timezone other than UTC. + date_default_timezone_set('Asia/Tokyo'); // +09:00 - date_default_timezone_set($tz); + $timestamp = strtotime('2017-03-18 midnight'); $time = Time::createFromTimestamp($timestamp); - $this->assertSame(date('2017-03-18 00:00:00'), $time->toDateTimeString()); + $this->assertSame('Asia/Tokyo', $time->getTimezone()->getName()); + $this->assertSame('2017-03-18 00:00:00', $time->format('Y-m-d H:i:s')); + + // Restore timezone. + date_default_timezone_set($tz); } public function testCreateFromTimestampWithTimezone(): void From c52f3086f047d261e2a4fea8f5b98a4788a88cbb Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 15 Feb 2024 08:20:40 +0900 Subject: [PATCH 2/2] docs: add user guide --- user_guide_src/source/changelogs/v4.4.6.rst | 9 +++++++++ user_guide_src/source/installation/upgrade_446.rst | 14 ++++++++++++++ user_guide_src/source/libraries/time.rst | 5 +++++ 3 files changed, 28 insertions(+) diff --git a/user_guide_src/source/changelogs/v4.4.6.rst b/user_guide_src/source/changelogs/v4.4.6.rst index 1fbee1a6eb65..234536b9d817 100644 --- a/user_guide_src/source/changelogs/v4.4.6.rst +++ b/user_guide_src/source/changelogs/v4.4.6.rst @@ -14,6 +14,15 @@ Release Date: Unreleased BREAKING ******** +Time::createFromTimestamp() +=========================== + +A bug that caused :ref:`Time::createFromTimestamp() ` +to return a Time instance with a timezone of UTC has been fixed. + +Starting with this version, when you do not specify a timezone, a Time instance +with the app's timezone is returned by default. + *************** Message Changes *************** diff --git a/user_guide_src/source/installation/upgrade_446.rst b/user_guide_src/source/installation/upgrade_446.rst index a010fb8f7b7e..c19b814ba74d 100644 --- a/user_guide_src/source/installation/upgrade_446.rst +++ b/user_guide_src/source/installation/upgrade_446.rst @@ -20,6 +20,20 @@ Mandatory File Changes Breaking Changes **************** +Time::createFromTimestamp() Timezone Change +=========================================== + +When you do not specify a timezone, now +:ref:`Time::createFromTimestamp() ` returns a Time +instance with the app's timezone is returned. + +If you want to keep the timezone UTC, you need to call ``setTimezone('UTC')``:: + + use CodeIgniter\I18n\Time; + + $time = Time::createFromTimestamp(1501821586)->setTimezone('UTC'); + + ********************* Breaking Enhancements ********************* diff --git a/user_guide_src/source/libraries/time.rst b/user_guide_src/source/libraries/time.rst index 2175a8c681a3..668a7bebbe7c 100644 --- a/user_guide_src/source/libraries/time.rst +++ b/user_guide_src/source/libraries/time.rst @@ -114,6 +114,8 @@ and returns a ``Time`` instance, instead of DateTimeImmutable: .. literalinclude:: time/011.php +.. _time-createfromtimestamp: + createFromTimestamp() ===================== @@ -121,6 +123,9 @@ This method takes a UNIX timestamp and, optionally, the timezone and locale, to .. literalinclude:: time/012.php +.. note:: Due to a bug, prior to v4.4.6, this method returned a Time instance + in timezone UTC when you do not specify a timezone. + createFromInstance() ====================