Skip to content

Commit

Permalink
Merge pull request #3014 from briannesbitt/fix/issue-3006-set-test-now
Browse files Browse the repository at this point in the history
Fallback to default timezone for mocked now
  • Loading branch information
kylekatarnls committed May 1, 2024
2 parents 3daf68d + e89692a commit 8ff64b9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Carbon/Traits/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ private function mockConstructorParameters(&$time, ?CarbonTimeZone $timezone): v
return;
}

if ($timezone) {
$testInstance = $testInstance->setTimezone($timezone);
if ($testInstance instanceof DateTimeInterface) {
$testInstance = $testInstance->setTimezone($timezone ?? date_default_timezone_get());
}

if (static::hasRelativeKeywords($time)) {
Expand Down
17 changes: 17 additions & 0 deletions tests/Carbon/TestingAidsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,23 @@ public function modify($modify)
$this->assertSame('2000-01-02 00:00:00 UTC', $currentTime->format('Y-m-d H:i:s e'));
}

public function testTimezoneConsistency()
{
Carbon::setTestNow();
date_default_timezone_set('UTC');
$currentDate = Carbon::now()->setTimezone('America/Los_Angeles');
$laDate = $currentDate->format('Y-m-d H:i:s e');
$utcDate = $currentDate->copy()->utc()->format('Y-m-d H:i:s e');

Carbon::setTestNow($currentDate);
$this->assertSame($utcDate, Carbon::now()->format('Y-m-d H:i:s e'));
$this->assertSame($utcDate, Carbon::now('UTC')->format('Y-m-d H:i:s e'));

Carbon::setTestNowAndTimezone($currentDate);
$this->assertSame($laDate, Carbon::now()->format('Y-m-d H:i:s e'));
$this->assertSame($utcDate, Carbon::now('UTC')->format('Y-m-d H:i:s e'));
}

public function testSleep()
{
$initial = Carbon::now('UTC');
Expand Down

0 comments on commit 8ff64b9

Please sign in to comment.