Skip to content

fix: Time::now() does not respect timezone when using setTestNow() #6752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions system/I18n/TimeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ public function __construct(?string $time = null, $timezone = null, ?string $loc

// If a test instance has been provided, use it instead.
if ($time === '' && static::$testNow instanceof self) {
$timezone = $timezone ?: static::$testNow->getTimezone();
$time = static::$testNow->format('Y-m-d H:i:s');
if ($timezone !== null) {
$testNow = static::$testNow->setTimezone($timezone);
$time = $testNow->format('Y-m-d H:i:s');
} else {
$timezone = static::$testNow->getTimezone();
$time = static::$testNow->format('Y-m-d H:i:s');
}
}

$timezone = $timezone ?: date_default_timezone_get();
Expand Down Expand Up @@ -310,7 +315,7 @@ public function toDateTime()

/**
* Creates an instance of Time that will be returned during testing
* when calling 'Time::now' instead of the current time.
* when calling 'Time::now()' instead of the current time.
*
* @param DateTimeInterface|self|string|null $datetime
* @param DateTimeZone|string|null $timezone
Expand Down
9 changes: 9 additions & 0 deletions tests/system/I18n/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,15 @@ public function testUnserializeTimeObject()
$this->assertNotSame($time1, $time2);
}

public function testSetTestNowWithTimeZone()
{
Time::setTestNow('2017/03/10 12:00', 'Asia/Tokyo');

$now = Time::now('UTC');

$this->assertSame('2017-03-10T03:00:00+00:00', $now->format('c'));
}

public function testSetTestNowWithFaLocale()
{
Locale::setDefault('fa');
Expand Down