Skip to content

Commit 30b3008

Browse files
committed
fix(I18n): address review suggestions for Time::today/yesterday/tomorrow
Use new static(null) instead of static::now() to avoid coupling overridable extension points, and freeze time in testTodayLocalized to prevent intermittent failure across timezones.
1 parent 8aabfc3 commit 30b3008

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

‎system/I18n/TimeTrait.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public static function parse(string $datetime, $timezone = null, ?string $locale
144144
*/
145145
public static function today($timezone = null, ?string $locale = null)
146146
{
147-
return static::now($timezone, $locale)->setTime(0, 0, 0, 0);
147+
return (new static(null, $timezone, $locale))->setTime(0, 0, 0, 0);
148148
}
149149

150150
/**
@@ -158,7 +158,7 @@ public static function today($timezone = null, ?string $locale = null)
158158
*/
159159
public static function yesterday($timezone = null, ?string $locale = null)
160160
{
161-
return static::now($timezone, $locale)->modify('-1 day')->setTime(0, 0, 0, 0);
161+
return (new static(null, $timezone, $locale))->modify('-1 day')->setTime(0, 0, 0, 0);
162162
}
163163

164164
/**
@@ -172,7 +172,7 @@ public static function yesterday($timezone = null, ?string $locale = null)
172172
*/
173173
public static function tomorrow($timezone = null, ?string $locale = null)
174174
{
175-
return static::now($timezone, $locale)->modify('+1 day')->setTime(0, 0, 0, 0);
175+
return (new static(null, $timezone, $locale))->modify('+1 day')->setTime(0, 0, 0, 0);
176176
}
177177

178178
/**

‎tests/system/I18n/TimeLegacyTest.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,14 @@ public function testToday(): void
170170

171171
public function testTodayLocalized(): void
172172
{
173+
// Freeze time to avoid flakiness when server and London are on different calendar days
174+
TimeLegacy::setTestNow('2026-06-15 12:00:00', 'UTC');
175+
173176
$time = TimeLegacy::today('Europe/London');
174177

175-
$this->assertSame(date('Y-m-d 00:00:00'), $time->toDateTimeString());
178+
$this->assertSame('2026-06-15 00:00:00', $time->toDateTimeString());
179+
180+
TimeLegacy::setTestNow();
176181
}
177182

178183
public function testYesterday(): void

‎tests/system/I18n/TimeTest.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,14 @@ public function testToday(): void
171171

172172
public function testTodayLocalized(): void
173173
{
174+
// Freeze time to avoid flakiness when server and London are on different calendar days
175+
Time::setTestNow('2026-06-15 12:00:00', 'UTC');
176+
174177
$time = Time::today('Europe/London');
175178

176-
$this->assertSame(date('Y-m-d 00:00:00'), $time->toDateTimeString());
179+
$this->assertSame('2026-06-15 00:00:00', $time->toDateTimeString());
180+
181+
Time::setTestNow();
177182
}
178183

179184
public function testYesterday(): void

0 commit comments

Comments
 (0)