Skip to content

UnixTimeGenerator

UnixTimeGenerator generates bytes that combine a 48-bit timestamp in milliseconds since the Unix Epoch with 80 random bits

Code and concepts within this class are borrowed from the symfony/uid package and are used under the terms of the MIT license distributed with symfony/uid.

symfony/uid is copyright (c) Fabien Potencier.

See Also:

  • https://symfony.com/components/Uid - Symfony Uid component
  • https://github.com/symfony/uid/blob/4f9f537e57261519808a7ce1d941490736522bbc/UuidV7.php - Symfony UuidV7 class
  • https://github.com/symfony/uid/blob/6.2/LICENSE - MIT License

Properties

time

private static string $time
  • This property is static.

seed

private static ?string $seed
  • This property is static.

seedIndex

private static int $seedIndex
  • This property is static.

rand

private static int[] $rand
  • This property is static.

seedParts

private static int[] $seedParts
  • This property is static.

randomGenerator

private \Ramsey\Uuid\Generator\RandomGeneratorInterface $randomGenerator

intSize

private int $intSize

Methods

__construct

public __construct(\Ramsey\Uuid\Generator\RandomGeneratorInterface $randomGenerator, int $intSize = PHP_INT_SIZE): mixed

Parameters:

Parameter Type Description
$randomGenerator \Ramsey\Uuid\Generator\RandomGeneratorInterface
$intSize int

generate

Generate a binary string from a node ID, clock sequence, and current time

public generate(\Ramsey\Uuid\Type\Hexadecimal|int|string|null $node = null, int|null $clockSeq = null, \DateTimeInterface $dateTime = null): string

Parameters:

Parameter Type Description
$node \Ramsey\Uuid\Type\Hexadecimal|int|string|null Unused in this generator
$clockSeq int|null Unused in this generator
$dateTime \DateTimeInterface A date-time instance to use when
generating bytes

Return Value:

A binary string


randomize

private randomize(string $time): void

Parameters:

Parameter Type Description
$time string

increment

Special thanks to Nicolas Grekas for sharing the following information:

private increment(): string

Within the same ms, we increment the rand part by a random 24-bit number.

Instead of getting this number from random_bytes(), which is slow, we get it by sha512-hashing self::$seed. This produces 64 bytes of entropy, which we need to split in a list of 24-bit numbers. unpack() first splits them into 16 x 32-bit numbers; we take the first byte of each of these numbers to get 5 extra 24-bit numbers. Then, we consume those numbers one-by-one and run this logic every 21 iterations.

self::$rand holds the random part of the UUID, split into 5 x 16-bit numbers for x86 portability. We increment this random part by the next 24-bit number in the self::$seedParts list and decrement self::$seedIndex.

See Also:

  • https://twitter.com/nicolasgrekas/status/1583356938825261061 - Tweet from Nicolas Grekas


Automatically generated on 2025-03-18