Skip to content

Commit 1e90ad5

Browse files
committed
fix: Time::createFromTimestamp() loses microseconds
1 parent 75a9230 commit 1e90ad5

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

‎system/I18n/TimeTrait.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public static function createFromFormat($format, $datetime, $timezone = null)
265265
*/
266266
public static function createFromTimestamp(float|int $timestamp, $timezone = null, ?string $locale = null): static
267267
{
268-
$time = new self(gmdate('Y-m-d H:i:s', $timestamp), 'UTC', $locale);
268+
$time = new self('@' . $timestamp, 'UTC', $locale);
269269

270270
$timezone ??= 'UTC';
271271

‎tests/system/I18n/TimeTest.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,17 @@ public function testCreateFromTimestamp(): void
287287
date_default_timezone_set($tz);
288288
}
289289

290+
public function testCreateFromTimestampWithMicrotime(): void
291+
{
292+
$timestamp = 1489762800.654321;
293+
294+
// The timezone will be UTC if you don't specify.
295+
$time = Time::createFromTimestamp($timestamp);
296+
297+
// float cannot handle the number precisely.
298+
$this->assertSame('2017-03-17 15:00:00.654300', $time->format('Y-m-d H:i:s.u'));
299+
}
300+
290301
public function testCreateFromTimestampWithTimezone(): void
291302
{
292303
// Set the timezone temporarily to UTC to make sure the test timestamp is correct

0 commit comments

Comments
 (0)