Created
July 14, 2020 08:33
-
-
Save vudaltsov/9f3724d1ce13da895d878c6adef2a41a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Infrastructure\Uuid; | |
use Ramsey\Uuid\Nonstandard\UuidV6; | |
use Ramsey\Uuid\Rfc4122\UuidV1; | |
use Ramsey\Uuid\Uuid; | |
/** | |
* @psalm-pure | |
*/ | |
function extractUuidTime(string $uuid): \DateTimeImmutable | |
{ | |
$uuid = Uuid::fromString($uuid); | |
if (!$uuid instanceof UuidV1 && !$uuid instanceof UuidV6) { | |
throw new \InvalidArgumentException('Given string is not a time-based UUID.'); | |
} | |
return $uuid->getDateTime(); | |
} | |
/** | |
* @psalm-pure | |
*/ | |
function extractUuidMicroseconds(string $uuid): string | |
{ | |
return extractUuidTime($uuid)->format('Uu'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment