Skip to content

Commit 3c92e6e

Browse files
authored
Merge pull request #6752 from kenjis/fix-Time-setTestNow-timezone
fix: Time::now() does not respect timezone when using setTestNow()
2 parents 0fff1c9 + f34f8e3 commit 3c92e6e

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

system/I18n/TimeTrait.php

Lines changed: 8 additions & 3 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();
@@ -310,7 +315,7 @@ public function toDateTime()
310315

311316
/**
312317
* Creates an instance of Time that will be returned during testing
313-
* when calling 'Time::now' instead of the current time.
318+
* when calling 'Time::now()' instead of the current time.
314319
*
315320
* @param DateTimeInterface|self|string|null $datetime
316321
* @param DateTimeZone|string|null $timezone

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)