Core UUID generation and manipulation functions implementing RFC 9562.
Provides comprehensive support for all UUID versions (v0-v8) with efficient internal representation and conversion utilities.
Implementation inspired by clj-uuid by Dan Lentz: https://github.com/danlentz/clj-uuid Licensed under Eclipse Public License 1.0
Core UUID generation and manipulation functions implementing RFC 9562. Provides comprehensive support for all UUID versions (v0-v8) with efficient internal representation and conversion utilities. Implementation inspired by clj-uuid by Dan Lentz: https://github.com/danlentz/clj-uuid Licensed under Eclipse Public License 1.0
The MAX UUID is a special form of sentinel UUID that is specified to have all 128 bits set to one.
The MAX UUID is a special form of sentinel UUID that is specified to have all 128 bits set to one.
The NULL UUID is a special form of sentinel UUID that is specified to have all 128 bits set to zero.
The NULL UUID is a special form of sentinel UUID that is specified to have all 128 bits set to zero.
(< _)(< x y)(< x y & more)Directly compare two or more UUIDs for < relation.
Directly compare two or more UUIDs for < relation.
(= _)(= x y)(= x y & more)Directly compare two or more UUIDs for equality.
Directly compare two or more UUIDs for equality.
(> _)(> x y)(> x y & more)Directly compare two or more UUIDs for > relation.
Directly compare two or more UUIDs for > relation.
(monotonic-time)Return a monotonic timestamp (guaranteed always increasing) based on the number of 100-nanosecond intervals elapsed since the adoption of the Gregorian calendar in the West, 12:00am Friday October 15, 1582 UTC.
Return a monotonic timestamp (guaranteed always increasing) based on the number of 100-nanosecond intervals elapsed since the adoption of the Gregorian calendar in the West, 12:00am Friday October 15, 1582 UTC.
(squuid)Generate a SQUUID (sequential, random) unique identifier.
Generate a SQUUID (sequential, random) unique identifier.
A UUIDable object directly represents a UUID.
A UUIDable object directly represents a UUID.
(as-uuid x)Coerce the value 'x' to a UUID.
Coerce the value 'x' to a UUID.
(uuidable? x)Return 'true' if 'x' can be coerced to UUID.
Return 'true' if 'x' can be coerced to UUID.
A mechanism intended for user-level extension that defines the decoding rules for the local-part representation of arbitrary Clojure / Java Objects when used for computing namespaced identifiers.
A mechanism intended for user-level extension that defines the decoding rules for the local-part representation of arbitrary Clojure / Java Objects when used for computing namespaced identifiers.
(as-byte-array x)Extract a byte serialization that represents the 'name' of x, typically unique within a given namespace.
Extract a byte serialization that represents the 'name' of x, typically unique within a given namespace.
Protocol for RFC 9562 UUID operations.
Protocol for RFC 9562 UUID operations.
(get-instant uuid)Returns timestamp as java.util.Date, or nil for non-time-based UUIDs.
Returns timestamp as java.util.Date, or nil for non-time-based UUIDs.
(get-unix-time uuid)Returns timestamp as milliseconds since Unix epoch, or nil for non-time-based UUIDs.
Returns timestamp as milliseconds since Unix epoch, or nil for non-time-based UUIDs.
(get-node uuid)Returns node identifier: {:id long}
Returns node identifier: {:id long}
(hash-code uuid)Returns the hash code as a long.
Returns the hash code as a long.
(to-hex-string uuid)Returns 32-character hex string (no hyphens).
Returns 32-character hex string (no hyphens).
(uuid= x y)Returns true if UUIDs are equal.
Returns true if UUIDs are equal.
(to-byte-array uuid)Returns 16-byte array representation.
Returns 16-byte array representation.
(uuid> x y)Returns true if x > y (lexical byte-order comparison).
Returns true if x > y (lexical byte-order comparison).
(null? uuid)Returns true if this is the NULL UUID (all zeros).
Returns true if this is the NULL UUID (all zeros).
(get-time-fields uuid)Returns time components: {:low long :mid long :high long} Layout varies by version (v1 vs v6).
Returns time components: {:low long :mid long :high long}
Layout varies by version (v1 vs v6).(uuid< x y)Returns true if x < y (lexical byte-order comparison).
Returns true if x < y (lexical byte-order comparison).
(max? uuid)Returns true if this is the MAX UUID (all ones).
Returns true if this is the MAX UUID (all ones).
(to-string uuid)Returns canonical string: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Returns canonical string: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
(get-timestamp uuid)Extracts timestamp from time-based UUIDs (v1/v6/v7).
Returns:
MSB (64 bits): ┌─────────────────────────────────┬────────────────────┬──────────────┬──────┐ │ time_low (32 bits) │ time_mid (16 bits)│ time_hi (12) │ ver │ │ [bits 0-31 of timestamp] │ [bits 32-47] │ [bits 48-59] │ (4) │ └─────────────────────────────────┴────────────────────┴──────────────┴──────┘ Extract: time_low || time_mid || time_hi → 60-bit timestamp
MSB (64 bits): ┌──────────────────┬────────────────────┬──────────────┬──────┐ │ time_high (32) │ time_mid (16) │ time_low (12)│ ver │ │ [bits 28-59] │ [bits 12-27] │ [bits 0-11] │ (4) │ └──────────────────┴────────────────────┴──────────────┴──────┘ Bits: [63──────────────────────────16][15────────────12][11─8][7────0] Extract: time_hi(28) || time_mid(12) || time_lo(12) → 60-bit timestamp
MSB (64 bits): ┌─────────────────────────────────────────────────┬──────┬──────────────┐ │ unix_ts_ms (48 bits) │ ver │ rand (12) │ │ milliseconds since Unix epoch 1970-01-01 │ (4) │ │ └─────────────────────────────────────────────────┴──────┴──────────────┘ Bits: [63──────────────────────────────16][15──12][11────────────────0] Extract: bits [63:16] → 48-bit timestamp
Extracts timestamp from time-based UUIDs (v1/v6/v7). Returns: - v1/v6: 60-bit value (100-nanosecond intervals since Gregorian epoch 1582-10-15) - v7: 48-bit value (milliseconds since Unix epoch 1970-01-01) - Other versions: nil ## Bit Layout Diagrams ### UUID v1 (timestamp scattered across MSB) MSB (64 bits): ┌─────────────────────────────────┬────────────────────┬──────────────┬──────┐ │ time_low (32 bits) │ time_mid (16 bits)│ time_hi (12) │ ver │ │ [bits 0-31 of timestamp] │ [bits 32-47] │ [bits 48-59] │ (4) │ └─────────────────────────────────┴────────────────────┴──────────────┴──────┘ Extract: time_low || time_mid || time_hi → 60-bit timestamp ### UUID v6 (timestamp reordered for lexical sorting) MSB (64 bits): ┌──────────────────┬────────────────────┬──────────────┬──────┐ │ time_high (32) │ time_mid (16) │ time_low (12)│ ver │ │ [bits 28-59] │ [bits 12-27] │ [bits 0-11] │ (4) │ └──────────────────┴────────────────────┴──────────────┴──────┘ Bits: [63──────────────────────────16][15────────────12][11─8][7────0] Extract: time_hi(28) || time_mid(12) || time_lo(12) → 60-bit timestamp ### UUID v7 (Unix timestamp at start for optimal sorting) MSB (64 bits): ┌─────────────────────────────────────────────────┬──────┬──────────────┐ │ unix_ts_ms (48 bits) │ ver │ rand (12) │ │ milliseconds since Unix epoch 1970-01-01 │ (4) │ │ └─────────────────────────────────────────────────┴──────┴──────────────┘ Bits: [63──────────────────────────────16][15──12][11────────────────0] Extract: bits [63:16] → 48-bit timestamp
(to-urn-string uuid)Returns URN format: urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Returns URN format: urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
(get-version uuid)Returns the version number (0-8).
Returns the version number (0-8).
(get-words uuid)Returns UUID as two 64-bit words: {:high long :low long}
Returns UUID as two 64-bit words: {:high long :low long}
(get-variant uuid)Returns the variant number (typically 2 for RFC 9562).
Returns the variant number (typically 2 for RFC 9562).
(uuid? x)Returns true if x is a UUID instance.
Returns true if x is a UUID instance.
(get-clock-fields uuid)Returns clock components: {:seq short :high long :low long} seq is non-nil only for v1/v6.
Returns clock components: {:seq short :high long :low long}
seq is non-nil only for v1/v6.(to-uri uuid)Returns UUID as java.net.URI in URN format.
Returns UUID as java.net.URI in URN format.
(v1)Generate a v1 (time-based) unique identifier.
Generate a v1 (time-based) unique identifier.
(v3 context local-part)Generate a v3 (name based, MD5 hash) UUID.
Generate a v3 (name based, MD5 hash) UUID.
(v5 context local-part)Generate a v5 (name based, SHA1 hash) UUID.
Generate a v5 (name based, SHA1 hash) UUID.
(v6)Generate a v6 (time-based), lexically sortable, unique identifier.
Generate a v6 (time-based), lexically sortable, unique identifier.
(v7)Generate a v7 unix time-based, lexically sortable UUID.
Generate a v7 unix time-based, lexically sortable UUID.
(v8 msb lsb)Generate a v8 custom UUID with user-defined data.
The v8 UUID format provides 122 bits of custom data:
MSB layout (64 bits total): ┌──────────────────────────────┬────┬──────────────────┐ │ custom data (48) │ver │ custom data (12)│ │ │ 8 │ │ └──────────────────────────────┴────┴──────────────────┘ bits: [63──────────16][15─12][11────────────────────0]
LSB layout (64 bits total): ┌─────┬──────────────────────────────────────────────┐ │var │ custom data (62 bits) │ │ 2 │ │ └─────┴──────────────────────────────────────────────┘ bits: [63-62][61────────────────────────────────────0]
Total custom data: 48 + 12 + 62 = 122 bits
Note: Version and variant bits are automatically set correctly. Any existing version/variant bits in the input will be overwritten.
Args: msb - Most significant 64 bits (bits 12-15 will be set to version 8) lsb - Least significant 64 bits (bits 62-63 will be set to variant 2)
Example: (v8 0x0123456789abcdef 0xfedcba9876543210) ;=> #uuid "01234567-89ab-8def-bfed-cba987654321" ^^^^ ^^^^ version variant
Generate a v8 custom UUID with user-defined data.
The v8 UUID format provides 122 bits of custom data:
MSB layout (64 bits total):
┌──────────────────────────────┬────┬──────────────────┐
│ custom data (48) │ver │ custom data (12)│
│ │ 8 │ │
└──────────────────────────────┴────┴──────────────────┘
bits: [63──────────16][15─12][11────────────────────0]
LSB layout (64 bits total):
┌─────┬──────────────────────────────────────────────┐
│var │ custom data (62 bits) │
│ 2 │ │
└─────┴──────────────────────────────────────────────┘
bits: [63-62][61────────────────────────────────────0]
Total custom data: 48 + 12 + 62 = 122 bits
Note: Version and variant bits are automatically set correctly.
Any existing version/variant bits in the input will be overwritten.
Args:
msb - Most significant 64 bits (bits 12-15 will be set to version 8)
lsb - Least significant 64 bits (bits 62-63 will be set to variant 2)
Example:
(v8 0x0123456789abcdef 0xfedcba9876543210)
;=> #uuid "01234567-89ab-8def-bfed-cba987654321"
^^^^ ^^^^
version variantcljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |