Skip to content

Commit 4791433

Browse files
authored
fix: restore the timezone, locale and string format when unserializing Time (#10682)
1 parent bf13e17 commit 4791433

4 files changed

Lines changed: 44 additions & 3 deletions

File tree

‎system/I18n/TimeTrait.php‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,8 +1237,6 @@ public function __isset($name): bool
12371237
}
12381238

12391239
/**
1240-
* This is called when we unserialize the Time object.
1241-
*
12421240
* @param array<mixed, mixed> $data
12431241
*/
12441242
public function __unserialize(array $data): void
@@ -1247,6 +1245,16 @@ public function __unserialize(array $data): void
12471245
$timezone = $data['timezone'] ?? null;
12481246
assert(is_string($date) && is_string($timezone));
12491247

1250-
parent::__construct($date, new DateTimeZone($timezone));
1248+
$locale = $data["\0*\0locale"] ?? null;
1249+
$toStringFormat = $data["\0*\0toStringFormat"] ?? null;
1250+
1251+
$this->timezone = new DateTimeZone($timezone);
1252+
$this->locale = is_string($locale) ? $locale : Locale::getDefault();
1253+
1254+
if (is_string($toStringFormat)) {
1255+
$this->toStringFormat = $toStringFormat;
1256+
}
1257+
1258+
parent::__construct($date, $this->timezone);
12511259
}
12521260
}

‎tests/system/I18n/TimeLegacyTest.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,15 @@ public function testUnserializeTimeObject(): void
11471147
$this->assertNotSame($time1, $time2);
11481148
}
11491149

1150+
public function testUnserializeRestoresTimezoneAndLocale(): void
1151+
{
1152+
$time = unserialize(serialize(TimeLegacy::parse('2024-03-01 10:00:00', 'Europe/Paris', 'fr_FR')));
1153+
1154+
$this->assertSame('Europe/Paris', $time->getTimezoneName());
1155+
$this->assertSame('mars', $time->toLocalizedString('MMMM'));
1156+
$this->assertSame(1, $time->difference(TimeLegacy::parse('2024-03-02 10:00:00', 'Europe/Paris'))->getDays());
1157+
}
1158+
11501159
public function testSetTestNowWithFaLocale(): void
11511160
{
11521161
Locale::setDefault('fa');

‎tests/system/I18n/TimeTest.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,29 @@ public function testUnserializeTimeObject(): void
13581358
$this->assertNotSame($time1, $time2);
13591359
}
13601360

1361+
public function testUnserializeRestoresTimezoneAndLocale(): void
1362+
{
1363+
$time = unserialize(serialize(Time::parse('2024-03-01 10:00:00', 'Europe/Paris', 'fr_FR')));
1364+
1365+
$this->assertSame('Europe/Paris', $time->getTimezoneName());
1366+
$this->assertSame('mars', $time->toLocalizedString('MMMM'));
1367+
$this->assertSame(1, $time->difference(Time::parse('2024-03-02 10:00:00', 'Europe/Paris'))->getDays());
1368+
}
1369+
1370+
public function testUnserializeFallsBackWithoutTraitProperties(): void
1371+
{
1372+
$payload = sprintf(
1373+
'O:%d:"%s":3:{s:4:"date";s:26:"2024-03-01 10:00:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:12:"Europe/Paris";}',
1374+
strlen(Time::class),
1375+
Time::class,
1376+
);
1377+
$time = unserialize($payload);
1378+
1379+
$this->assertSame('Europe/Paris', $time->getTimezoneName());
1380+
$this->assertSame('March', $time->toLocalizedString('MMMM'));
1381+
$this->assertSame('2024-03-01 10:00:00', (string) $time);
1382+
}
1383+
13611384
public function testSetTestNowWithTimeZone(): void
13621385
{
13631386
Time::setTestNow('2017/03/10 12:00', 'Asia/Tokyo');

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Bugs Fixed
5151
- **HTTP:** Fixed a bug where ``Negotiate::media()`` threw an ``ErrorException`` when the ``Accept`` header contained a value without a subtype, such as ``json`` or an empty list element. Such values are now ignored.
5252
- **HTTP:** Fixed a bug where ``Negotiate`` could select a value that the request header excluded with ``q=0`` through a wildcard or a broader range. A ``q=0`` range now excludes every value it matches, such as ``text/html`` and ``text/html;charset=utf-8`` for ``text/*;q=0``, or ``en-US`` for ``en;q=0``, unless a more specific range in the header accepts the value. For example, ``Accept-Encoding: gzip;q=0, *`` no longer selects ``gzip``.
5353
- **HTTP:** Fixed stale ``$_REQUEST`` values after ``SiteURIFactory`` normalizes GET parameters, so ``Validation::withRequest()`` reads the normalized values while unrelated REQUEST entries are preserved.
54+
- **I18n:** Fixed a regression in v4.7.1 where ``Time`` and ``TimeLegacy`` lost their timezone, locale and ``__toString()`` format after ``unserialize()``, so methods such as ``difference()`` and ``getTimezoneName()`` threw an ``Error``.
5455
- **Security:** Fixed a bug where a valid JSON request body that is not an object (for example a JSON array) was corrupted after CSRF verification when the token was sent via the ``X-CSRF-TOKEN`` header.
5556

5657
See the repo's

0 commit comments

Comments
 (0)