View Source UUUIDv7 (UUUIDv7 v0.1.0)

UUUIDv7 for Elixir.

Used for generating version 7 UUIDs using submicrosecond precision. Normally the default precision is 1 millisecond, this causes issues when generating UUIDs in bulk because we can generate multiple UUIDs in the same millisecond. This module allows you to generate UUIDs with submicrosecond precision.

UUUIDv7.generate() "018e90d8-06e8-7f9f-bfd7-6730ba98a51b"

UUUIDv7.bingenerate() <<1, 142, 144, 216, 6, 232, 127, 159, 191, 215, 103, 48, 186, 152, 165, 27>>

Summary

Types

A raw binary representation of a UUID.

t()

A hex-encoded UUID string.

Functions

Generates a version 7 UUID in the binary format.

Decode a string representation of a UUID to the raw binary version.

Encode a raw UUID to the string representation.

Extract the millisecond timestamp from the UUID.

Generates a version 7 UUID using submilliseconds for increased clock precision.

returns the current time in milliseconds and scaled nanoseconds

scales the remaining nanoseconds to fit in 12 bits

Types

@type raw() :: <<_::128>>

A raw binary representation of a UUID.

@type t() :: <<_::288>>

A hex-encoded UUID string.

Functions

Generates a version 7 UUID in the binary format.

Example

iex> [UUUIDv7.bingenerate(), UUUIDv7.bingenerate()] iex> |> Enum.map(& :binary.part(&1, 0, 5)) iex> |> Enum.reduce(fn second, first -> second == first end) true

@spec decode(t()) :: raw() | :error

Decode a string representation of a UUID to the raw binary version.

Example

iex> UUUIDv7.decode("018e90d8-06e8-7f9f-bfd7-6730ba98a51b")
<<1, 142, 144, 216, 6, 232, 127, 159, 191, 215, 103, 48, 186, 152, 165, 27>>
@spec encode(raw()) :: t()

Encode a raw UUID to the string representation.

Example

iex> UUUIDv7.encode(<<1, 142, 144, 216, 6, 232, 127, 159, 191, 215, 103, 48, 186, 152, 165, 27>>)
"018e90d8-06e8-7f9f-bfd7-6730ba98a51b"
@spec extract_timestamp(t() | raw()) :: integer()

Extract the millisecond timestamp from the UUID.

Example

iex> UUUIDv7.extract_timestamp("018ecb40-c457-73e6-a400-000398daddd9")
1712807003223
@spec generate() :: t()

Generates a version 7 UUID using submilliseconds for increased clock precision.

Example

iex> [UUUIDv7.generate(), UUUIDv7.generate()] iex> |> Enum.map(& String.byte_slice(&1, 0, 11)) iex> |> Enum.reduce(fn second, first -> second == first end) true

returns the current time in milliseconds and scaled nanoseconds

Link to this function

scale_nanoseconds(nanos)

View Source

scales the remaining nanoseconds to fit in 12 bits

Example

iex> UUUIDv7.scale_nanoseconds(0)
0
iex> UUUIDv7.scale_nanoseconds(500000)
2048
iex> UUUIDv7.scale_nanoseconds(999999)
0xFFF