Skip to content

Commit fcd8597

Browse files
authored
Merge pull request #8544 from kenjis/fix-Time-createFromTimestamp
fix: Time::createFromTimestamp() returns Time with UTC
2 parents f78b67a + c52f308 commit fcd8597

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

system/I18n/TimeTrait.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ public static function createFromFormat($format, $datetime, $timezone = null)
266266
public static function createFromTimestamp(int $timestamp, $timezone = null, ?string $locale = null)
267267
{
268268
$time = new self(gmdate('Y-m-d H:i:s', $timestamp), 'UTC', $locale);
269-
$timezone ??= 'UTC';
269+
270+
$timezone ??= date_default_timezone_get();
270271

271272
return $time->setTimezone($timezone);
272273
}

tests/system/I18n/TimeTest.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -259,17 +259,21 @@ public function testCreateFromFormatWithInvalidFormat(): void
259259

260260
public function testCreateFromTimestamp(): void
261261
{
262-
// Set the timezone temporarily to UTC to make sure the test timestamp is correct
262+
// Save the current timezone.
263263
$tz = date_default_timezone_get();
264-
date_default_timezone_set('UTC');
265264

266-
$timestamp = strtotime('2017-03-18 midnight');
265+
// Change the timezone other than UTC.
266+
date_default_timezone_set('Asia/Tokyo'); // +09:00
267267

268-
date_default_timezone_set($tz);
268+
$timestamp = strtotime('2017-03-18 midnight');
269269

270270
$time = Time::createFromTimestamp($timestamp);
271271

272-
$this->assertSame(date('2017-03-18 00:00:00'), $time->toDateTimeString());
272+
$this->assertSame('Asia/Tokyo', $time->getTimezone()->getName());
273+
$this->assertSame('2017-03-18 00:00:00', $time->format('Y-m-d H:i:s'));
274+
275+
// Restore timezone.
276+
date_default_timezone_set($tz);
273277
}
274278

275279
public function testCreateFromTimestampWithTimezone(): void

user_guide_src/source/changelogs/v4.4.6.rst

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ Release Date: Unreleased
1414
BREAKING
1515
********
1616

17+
Time::createFromTimestamp()
18+
===========================
19+
20+
A bug that caused :ref:`Time::createFromTimestamp() <time-createfromtimestamp>`
21+
to return a Time instance with a timezone of UTC has been fixed.
22+
23+
Starting with this version, when you do not specify a timezone, a Time instance
24+
with the app's timezone is returned by default.
25+
1726
***************
1827
Message Changes
1928
***************

user_guide_src/source/installation/upgrade_446.rst

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ Mandatory File Changes
2020
Breaking Changes
2121
****************
2222

23+
Time::createFromTimestamp() Timezone Change
24+
===========================================
25+
26+
When you do not specify a timezone, now
27+
:ref:`Time::createFromTimestamp() <time-createfromtimestamp>` returns a Time
28+
instance with the app's timezone is returned.
29+
30+
If you want to keep the timezone UTC, you need to call ``setTimezone('UTC')``::
31+
32+
use CodeIgniter\I18n\Time;
33+
34+
$time = Time::createFromTimestamp(1501821586)->setTimezone('UTC');
35+
36+
2337
*********************
2438
Breaking Enhancements
2539
*********************

user_guide_src/source/libraries/time.rst

+5
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,18 @@ and returns a ``Time`` instance, instead of DateTimeImmutable:
114114

115115
.. literalinclude:: time/011.php
116116

117+
.. _time-createfromtimestamp:
118+
117119
createFromTimestamp()
118120
=====================
119121

120122
This method takes a UNIX timestamp and, optionally, the timezone and locale, to create a new Time instance:
121123

122124
.. literalinclude:: time/012.php
123125

126+
.. note:: Due to a bug, prior to v4.4.6, this method returned a Time instance
127+
in timezone UTC when you do not specify a timezone.
128+
124129
createFromInstance()
125130
====================
126131

0 commit comments

Comments
 (0)