Skip to content

Commit bd2635c

Browse files
committed
fix: Time::createFromTimestamp() returns Time with UTC
1 parent 7d50bd3 commit bd2635c

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
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

+8-4
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('Asia/Tokyo', $time->getTimezone()->getName());
272273
$this->assertSame(date('2017-03-18 00:00:00'), $time->toDateTimeString());
274+
275+
// Restore timezone.
276+
date_default_timezone_set($tz);
273277
}
274278

275279
public function testCreateFromTimestampWithTimezone(): void

0 commit comments

Comments
 (0)