Skip to content

Instantly share code, notes, and snippets.

@vudaltsov
Created July 14, 2020 08:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vudaltsov/9f3724d1ce13da895d878c6adef2a41a to your computer and use it in GitHub Desktop.
Save vudaltsov/9f3724d1ce13da895d878c6adef2a41a to your computer and use it in GitHub Desktop.
<?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