Skip to content

Commit cedf78d

Browse files
committed
fix(I18n): fix timezone and testNow calculation in Time::today(), yesterday(), and tomorrow()
- Compute start of day relative to the requested timezone instead of the server default timezone - Respect Time::setTestNow() mock instance in today(), yesterday(), and tomorrow() - Add unit tests covering date boundary transitions across different timezones - Add changelog entry in v4.7.5.rst
1 parent 501706c commit cedf78d

3 files changed

Lines changed: 55 additions & 3 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 new static(date('Y-m-d 00:00:00'), $timezone, $locale);
147+
return static::now($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 new static(date('Y-m-d 00:00:00', strtotime('-1 day')), $timezone, $locale);
161+
return static::now($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 new static(date('Y-m-d 00:00:00', strtotime('+1 day')), $timezone, $locale);
175+
return static::now($timezone, $locale)->modify('+1 day')->setTime(0, 0, 0, 0);
176176
}
177177

178178
/**

‎tests/system/I18n/TimeTest.php‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,57 @@ public function testTomorrow(): void
190190
$this->assertSame(date('Y-m-d 00:00:00', strtotime('+1 day')), $time->toDateTimeString());
191191
}
192192

193+
public function testTodayWithTimezoneAcrossDateBoundary(): void
194+
{
195+
// When UTC is 2026-09-07 23:30:00, in Asia/Tokyo (+09:00) it is already 2026-09-08 08:30:00
196+
Time::setTestNow('2026-09-07 23:30:00', 'UTC');
197+
198+
$tokyoToday = Time::today('Asia/Tokyo');
199+
$this->assertSame('2026-09-08 00:00:00', $tokyoToday->toDateTimeString());
200+
201+
// When UTC is 2026-09-08 02:00:00, in America/New_York (-04:00 EDT) it is still 2026-09-07 22:00:00
202+
Time::setTestNow('2026-09-08 02:00:00', 'UTC');
203+
204+
$nyToday = Time::today('America/New_York');
205+
$this->assertSame('2026-09-07 00:00:00', $nyToday->toDateTimeString());
206+
207+
Time::setTestNow();
208+
}
209+
210+
public function testYesterdayWithTimezoneAcrossDateBoundary(): void
211+
{
212+
// When UTC is 2026-09-07 23:30:00, in Tokyo it is 2026-09-08, so Tokyo yesterday is 2026-09-07
213+
Time::setTestNow('2026-09-07 23:30:00', 'UTC');
214+
215+
$tokyoYesterday = Time::yesterday('Asia/Tokyo');
216+
$this->assertSame('2026-09-07 00:00:00', $tokyoYesterday->toDateTimeString());
217+
218+
// When UTC is 2026-09-08 02:00:00, in NY it is 2026-09-07, so NY yesterday is 2026-09-06
219+
Time::setTestNow('2026-09-08 02:00:00', 'UTC');
220+
221+
$nyYesterday = Time::yesterday('America/New_York');
222+
$this->assertSame('2026-09-06 00:00:00', $nyYesterday->toDateTimeString());
223+
224+
Time::setTestNow();
225+
}
226+
227+
public function testTomorrowWithTimezoneAcrossDateBoundary(): void
228+
{
229+
// When UTC is 2026-09-07 23:30:00, in Tokyo it is 2026-09-08, so Tokyo tomorrow is 2026-09-09
230+
Time::setTestNow('2026-09-07 23:30:00', 'UTC');
231+
232+
$tokyoTomorrow = Time::tomorrow('Asia/Tokyo');
233+
$this->assertSame('2026-09-09 00:00:00', $tokyoTomorrow->toDateTimeString());
234+
235+
// When UTC is 2026-09-08 02:00:00, in NY it is 2026-09-07, so NY tomorrow is 2026-09-08
236+
Time::setTestNow('2026-09-08 02:00:00', 'UTC');
237+
238+
$nyTomorrow = Time::tomorrow('America/New_York');
239+
$this->assertSame('2026-09-08 00:00:00', $nyTomorrow->toDateTimeString());
240+
241+
Time::setTestNow();
242+
}
243+
193244
public function testCreateFromDate(): void
194245
{
195246
$time = Time::createFromDate(2017, 03, 05, 'America/Chicago');

‎user_guide_src/source/changelogs/v4.7.5.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Bugs Fixed
4646
- **Files:** Fixed a bug where ``File::move()`` and ``UploadedFile::move()`` set executable and overly permissive file permissions (``0777 & ~umask()`` instead of ``0666 & ~umask()``), and ``UploadedFile::move()`` targeted the parent directory instead of the destination file for ``chmod()``.
4747
- **Helpers:** Fixed a bug where ``get_dir_file_info()`` returned incomplete entries for subdirectories and missing files instead of omitting them.
4848
- **Honeypot:** Fixed a bug where bot detection returned an HTTP 500 response instead of 403 (Forbidden).
49+
- **I18n:** Fixed a bug where ``Time::today()``, ``Time::yesterday()``, and ``Time::tomorrow()`` ignored the specified ``$timezone`` and ``setTestNow()`` when calculating the day.
4950
- **Logger:** Fixed a bug where interpolating a log message with array or non-stringable context values could raise PHP warnings or errors.
5051
- **Cache:** Fixed ``MemcachedHandler::decrement()`` initializing a non-existent counter to the positive offset. Missing counters are now initialized to ``0``, reflecting Memcached's unsigned, saturating counter semantics.
5152

0 commit comments

Comments
 (0)