Skip to content

Commit

Permalink
Merge pull request #2369 from kylekatarnls/fix/create-from-timestamp-…
Browse files Browse the repository at this point in the history
…microsecond

Fix #2368 pad left trailing zeros in createFromTimestamp()
  • Loading branch information
kylekatarnls committed Jun 28, 2021
2 parents 0dbe095 + 1c968a6 commit f47f17d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Carbon/Traits/Timestamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static function createFromTimestampUTC($timestamp)
$delta = floor($decimal / static::MICROSECONDS_PER_SECOND);
$integer += $delta;
$decimal -= $delta * static::MICROSECONDS_PER_SECOND;
$decimal = str_pad((string) $decimal, 6, '0', STR_PAD_LEFT);

return static::rawCreateFromFormat('U u', "$integer $decimal");
}
Expand Down
8 changes: 8 additions & 0 deletions tests/CarbonImmutable/CreateFromTimestampTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ public function testCreateFromMicrotimeFloat()
$this->assertSame('America/Toronto', $d->tzName);
$this->assertSame('2020-09-23 14:52:44.889523', $d->format('Y-m-d H:i:s.u'));
$this->assertSame('1600887164.889523', $d->format('U.u'));

$microtime = 1600887164.0603;
$d = Carbon::createFromTimestamp($microtime);
$this->assertSame('America/Toronto', $d->tzName);
$this->assertSame('2020-09-23 14:52:44.060300', $d->format('Y-m-d H:i:s.u'));
$this->assertSame('1600887164.060300', $d->format('U.u'));

$this->assertSame('010000', Carbon::createFromTimestamp(0.01)->format('u'));
}

public function testCreateFromMicrotimeStrings()
Expand Down

0 comments on commit f47f17d

Please sign in to comment.