Skip to content

Commit

Permalink
- improved timestamps handling
Browse files Browse the repository at this point in the history
- improved toDateTime() method
- added strictness
- typos
  • Loading branch information
kodeart committed Apr 5, 2023
1 parent 19033bb commit 7b3b0b8
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions ULID.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

/*
* This file is part of the Koded package.
Expand Down Expand Up @@ -72,7 +72,7 @@ private function __construct(int $timestamps, int $count)
}

/**
* Creates and instance of ULID with number
* Creates an instance of ULID with number
* of timestamps defined by the count argument.
* @param int $count
* @return static
Expand All @@ -83,7 +83,7 @@ public static function generate(int $count = 1): self
}

/**
* Decode the ULID string into the instance of ULID.
* Decode the ULID string into an instance of ULID.
* @param string $ulid
* @return static
*/
Expand All @@ -104,7 +104,7 @@ public static function fromULID(string $ulid): self
}

/**
* Decode the ULID string in UUID format into the instance of ULID.
* Decode the ULID string in UUID format into an instance of ULID.
* @param string $ulid UUID representation of ULID value
* @return static
*/
Expand All @@ -118,31 +118,37 @@ public static function fromUUID(string $ulid): self
}

/**
* Decode the date time string into the instance of ULID.
* @param float $timestamp UNIX timestamp with or without the milliseconds part.
* Decode the date time string into an instance of ULID.
* @param float $timestamp UNIX timestamp with or without the milliseconds part
* @return static
*/
public static function fromTimestamp(float $timestamp): self
{
if ($timestamp >= 0 && $timestamp <= PHP_INT_MAX) {
[$ts, $ms] = explode('.', $timestamp) + [1 => '000'];
return new static("$ts$ms", 1);
if ($timestamp <= 0 || $timestamp >= PHP_INT_MAX) {
throw new InvalidArgumentException("Invalid timestamp ($timestamp)", 400);
}
throw new InvalidArgumentException("Invalid timestamp ($timestamp)", 400);
$timestamp = (string)$timestamp;
if (str_contains($timestamp, '.')) {
$timestamp = (string)($timestamp * 1000);
}
if (strlen($timestamp) >= 13) {
$timestamp = substr($timestamp, 0, 13);
}
return new static(intval($timestamp), 1);
}

/**
* Decode the date time string into the instance of ULID.
* Decode the date time string into an instance of ULID.
* @param string $datetime in format: Y-m-d H:i:s with optional 'v' (milliseconds)
* @return static
*/
public static function fromDateTime(string $datetime): self
{
try {
$dt = (false === str_contains($datetime, '.'))
? DateTime::createFromFormat('Y-m-d H:i:s', $datetime, new DateTimeZone('UTC'))
: DateTime::createFromFormat('Y-m-d H:i:s.v', $datetime, new DateTimeZone('UTC'));
return new static($dt->getTimestamp() . $dt->format('v'), 1);
$dt = (str_contains($datetime, '.'))
? DateTime::createFromFormat('Y-m-d H:i:s.v', $datetime, new DateTimeZone('UTC'))
: DateTime::createFromFormat('Y-m-d H:i:s', $datetime, new DateTimeZone('UTC'));
return new static(intval($dt->getTimestamp() . $dt->format('v')), 1);
} catch (Throwable) {
throw new InvalidArgumentException("Invalid datetime ($datetime)", 400);
}
Expand All @@ -154,7 +160,7 @@ public static function valid(string $uuid): bool
}

/**
* Creates a single, or a list. of UUID representations of ULID values.
* Creates a single, or a list, of UUID values.
* @return array|string
*/
public function toUUID(): array|string
Expand All @@ -175,7 +181,7 @@ public function toUUID(): array|string
}

/**
* Creates a single, or a list. of ULID representations.
* Creates a single, or a list of ULID values.
* @return array|string
*/
public function toULID(): array|string
Expand All @@ -191,16 +197,17 @@ public function toULID(): array|string
}

/**
* Returns a single, or a list, of DateTime instances for this ULID.
* Returns a single, or a list, of DateTime instances.
* @return array|DateTime
*/
public function toDateTime(): array|DateTime
{
$list = [];
foreach ($this->timestamps as $timestamp) {
$timestamp = (string)$timestamp;
$datetime = new DateTime('@' . substr($timestamp, 0, 10), new DateTimeZone('UTC'));
if (13 === strlen($timestamp)) {
$ms = substr($timestamp, 10);
if (strlen($timestamp) >= 13) {
$ms = substr($timestamp, 10, 3);
$datetime->modify("+{$ms} milliseconds");
}
$list[] = $datetime;
Expand Down

0 comments on commit 7b3b0b8

Please sign in to comment.