Skip to content

Commit aa64c6b

Browse files
authored
fix: make Time::createFromTimestamp locale-independent (#10151)
1 parent 5a24c0c commit aa64c6b

3 files changed

Lines changed: 12 additions & 4 deletions

File tree

system/I18n/TimeTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public static function createFromFormat($format, $datetime, $timezone = null): s
263263
*/
264264
public static function createFromTimestamp(float|int $timestamp, $timezone = null, ?string $locale = null): static
265265
{
266-
$time = new static(sprintf('@%.6f', $timestamp), 'UTC', $locale);
266+
$time = new static(sprintf('@%.6F', $timestamp), 'UTC', $locale);
267267

268268
$timezone ??= 'UTC';
269269

tests/system/I18n/TimeTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,18 @@ public function testCreateFromTimestamp(): void
291291
public function testCreateFromTimestampWithMicroseconds(): void
292292
{
293293
$timestamp = 1489762800.654321;
294+
$locale = setlocale(LC_NUMERIC, '0');
294295

295-
// The timezone will be UTC if you don't specify.
296-
$time = Time::createFromTimestamp($timestamp);
296+
setlocale(LC_NUMERIC, 'de_DE.UTF-8', 'de_DE');
297+
298+
try {
299+
// The timezone will be UTC if you don't specify.
300+
$time = Time::createFromTimestamp($timestamp);
297301

298-
$this->assertSame('2017-03-17 15:00:00.654321', $time->format('Y-m-d H:i:s.u'));
302+
$this->assertSame('2017-03-17 15:00:00.654321', $time->format('Y-m-d H:i:s.u'));
303+
} finally {
304+
setlocale(LC_NUMERIC, $locale);
305+
}
299306
}
300307

301308
public function testCreateFromTimestampWithTimezone(): void

user_guide_src/source/changelogs/v4.7.3.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Bugs Fixed
4141
- **Commands:** Fixed a bug in the ``env`` command where passing options only would cause the command to throw a ``TypeError`` instead of showing the current environment.
4242
- **Common:** Fixed a bug where the ``command()`` helper function did not properly clean up output buffers, which could lead to risky tests when exceptions were thrown.
4343
- **Kint:** Fixed a bug where stale Content Security Policy nonces were reused in worker mode, causing browser CSP violations for Debug Toolbar assets.
44+
- **Time:** Fixed a bug where ``Time::createFromTimestamp()`` could fail for microsecond timestamps when ``LC_NUMERIC`` used a comma decimal separator.
4445
- **Validation:** Fixed a bug where ``Validation::getValidated()`` dropped fields whose validated value was explicitly ``null``.
4546

4647
See the repo's

0 commit comments

Comments
 (0)