View Source UUIDv7 (UUUIDv7 v0.1.1)
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
Functions
Generates a version 7 UUID in the binary format.
Decode a string representation of a UUID to the raw binary version.
Callback implementation for Ecto.Type.embed_as/1
.
Encode a raw UUID to the string representation.
Callback implementation for Ecto.Type.equal?/2
.
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
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>>
Callback implementation for Ecto.Type.embed_as/1
.
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"
Callback implementation for Ecto.Type.equal?/2
.
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
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