Skip to content

Commit

Permalink
Merge pull request #5588 from totoprayogo1916/update/Time
Browse files Browse the repository at this point in the history
fix: `Time::createFromTimestamp()` sets incorrect time when specifying timezone
  • Loading branch information
kenjis authored Jan 19, 2022
2 parents da58ec1 + a7dfa5f commit 0c5917c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion system/I18n/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,10 @@ public static function createFromFormat($format, $datetime, $timezone = null)
*/
public static function createFromTimestamp(int $timestamp, $timezone = null, ?string $locale = null)
{
return new self(gmdate('Y-m-d H:i:s', $timestamp), $timezone ?? 'UTC', $locale);
$time = new self(gmdate('Y-m-d H:i:s', $timestamp), 'UTC', $locale);
$timezone ??= 'UTC';

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

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/system/I18n/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,21 @@ public function testCreateFromTimestamp()
$this->assertSame(date('2017-03-18 00:00:00'), $time->toDateTimeString());
}

public function testCreateFromTimestampWithTimezone()
{
// Set the timezone temporarily to UTC to make sure the test timestamp is correct
$tz = date_default_timezone_get();
date_default_timezone_set('UTC');

$timestamp = strtotime('2017-03-18 midnight'); // in UTC

date_default_timezone_set($tz);

$time = Time::createFromTimestamp($timestamp, 'Asia/Jakarta'); // UTC +7

$this->assertSame(date('2017-03-18 07:00:00'), $time->toDateTimeString());
}

public function testTestNow()
{
$this->assertFalse(Time::hasTestNow());
Expand Down

0 comments on commit 0c5917c

Please sign in to comment.