Skip to content

Commit

Permalink
fix: Time::createFromTimestamp() returns Time with UTC
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Feb 14, 2024
1 parent 7d50bd3 commit b1418a1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion system/I18n/TimeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
14 changes: 9 additions & 5 deletions tests/system/I18n/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b1418a1

Please sign in to comment.