Skip to content

Commit

Permalink
Time(Immutable)::createFromTimestamp() now supports microsecond preci…
Browse files Browse the repository at this point in the history
…sion
  • Loading branch information
freost committed Aug 20, 2024
1 parent f05d463 commit d319315
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/mako/chrono/TimeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static function createFromDate(int $year, ?int $month = null, ?int $day =
*
* @return false|static
*/
public static function createFromTimestamp(int $timestamp, null|DateTimeZone|string $timeZone = null);
public static function createFromTimestamp(float|int $timestamp, null|DateTimeZone|string $timeZone = null);

/**
* Returns a new instance according to the specified DOS timestamp.
Expand Down
17 changes: 3 additions & 14 deletions src/mako/chrono/traits/TimeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public static function createFromDate(int $year, ?int $month = null, ?int $day =
*
* @return false|static
*/
public static function createFromTimestamp(int $timestamp, null|DateTimeZone|string $timeZone = null)
public static function createFromTimestamp(float|int $timestamp, null|DateTimeZone|string $timeZone = null)
{
return (new static('now', $timeZone))->setTimestamp($timestamp);
return static::createFromFormat(is_int($timestamp) ? 'U' : 'U.u', (string) $timestamp, $timeZone);
}

/**
Expand Down Expand Up @@ -91,18 +91,7 @@ public static function createFromDOSTimestamp(int $timestamp, null|DateTimeZone|
#[\ReturnTypeWillChange]
public static function createFromFormat(string $format, string $time, null|DateTimeZone|string $timeZone = null)
{
if ($timeZone !== null) {
if (($timeZone instanceof DateTimeZone) === false) {
$timeZone = new DateTimeZone($timeZone);
}

$dateTime = parent::createFromFormat($format, $time, $timeZone);
}
else {
$dateTime = parent::createFromFormat($format, $time);
}

return new static($dateTime->format('Y-m-d\TH:i:s.u'), $dateTime->getTimeZone());
return new static(parent::createFromFormat($format, $time)->format('Y-m-d\TH:i:s.u'), $timeZone);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/chrono/TimeImmutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ public function testCreateFromTimestamp(): void

//

$time = Time::createFromTimestamp(431093532.123);

$this->assertSame('431093532.123000', $time->format('U.u'));

//

$time = TimeImmutable::createFromTimestamp(431093532, 'Asia/Tokyo');

$this->assertSame('Asia/Tokyo', $time->getTimeZone()->getName());
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/chrono/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ public function testCreateFromTimestamp(): void

//

$time = Time::createFromTimestamp(431093532.123);

$this->assertSame('431093532.123000', $time->format('U.u'));

//

$time = Time::createFromTimestamp(431093532, 'Asia/Tokyo');

$this->assertSame('Asia/Tokyo', $time->getTimeZone()->getName());
Expand Down

0 comments on commit d319315

Please sign in to comment.