Created
November 16, 2024 03:00
-
-
Save priyadi/6046e99a67582639f9352313ac65c2ca to your computer and use it in GitHub Desktop.
Dated deterministic uuid
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); | |
use Symfony\Component\Uid\UuidV8; | |
class DatedDeterministicUuid extends UuidV8 | |
{ | |
public function __construct(\DateTimeInterface $date, mixed $value) | |
{ | |
$date = $date->diff(new \DateTimeImmutable('1970-01-01'))->format('%a'); | |
$date = dechex((int) $date); | |
$hash = hash('xxh128', serialize($value)); | |
$uuid = sprintf( | |
'%06s%2s-8%3s-%04x-%4s-%12s', | |
$date, | |
substr($hash, 0, 2), | |
substr($hash, 2, 3), | |
0x8000 | (((int) base_convert(substr($hash, 5, 4), 16, 10)) & 0x3FFF), | |
substr($hash, 9, 4), | |
substr($hash, 13, 12) | |
); | |
parent::__construct($uuid); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment