Skip to content

Commit d063c44

Browse files
committed
fix: Time::now() does not respect timezone when using setTestNow()
1 parent 0fff1c9 commit d063c44

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

system/I18n/TimeTrait.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,13 @@ public function __construct(?string $time = null, $timezone = null, ?string $loc
7777

7878
// If a test instance has been provided, use it instead.
7979
if ($time === '' && static::$testNow instanceof self) {
80-
$timezone = $timezone ?: static::$testNow->getTimezone();
81-
$time = static::$testNow->format('Y-m-d H:i:s');
80+
if ($timezone !== null) {
81+
$testNow = static::$testNow->setTimezone($timezone);
82+
$time = $testNow->format('Y-m-d H:i:s');
83+
} else {
84+
$timezone = static::$testNow->getTimezone();
85+
$time = static::$testNow->format('Y-m-d H:i:s');
86+
}
8287
}
8388

8489
$timezone = $timezone ?: date_default_timezone_get();

tests/system/I18n/TimeTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,15 @@ public function testUnserializeTimeObject()
11321132
$this->assertNotSame($time1, $time2);
11331133
}
11341134

1135+
public function testSetTestNowWithTimeZone()
1136+
{
1137+
Time::setTestNow('2017/03/10 12:00', 'Asia/Tokyo');
1138+
1139+
$now = Time::now('UTC');
1140+
1141+
$this->assertSame('2017-03-10T03:00:00+00:00', $now->format('c'));
1142+
}
1143+
11351144
public function testSetTestNowWithFaLocale()
11361145
{
11371146
Locale::setDefault('fa');

0 commit comments

Comments
 (0)