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