Portable UUIDv7 generator for Clojure, ClojureScript, Babashka, nbb, and scittle.
Implements RFC 9562 Section 6.2 Method 3 (monotonic random):
Usage: (require '[com.github.franks42.uuidv7.core :refer [uuidv7]]) (uuidv7) ;=> #uuid "0195xxxx-xxxx-7xxx-xxxx-xxxxxxxxxxxx"
;; Extract embedded data: (extract-ts u) ;=> 1738934578991 (ms since epoch) (extract-inst u) ;=> #inst "2025-02-07..." (as Date) (extract-key u) ;=> [ts a bh bl] (sortable composite key)
The 74-bit counter space (~1.9 * 10^22 values per millisecond) is effectively inexhaustible. On each new millisecond the counter reseeds with fresh random bits. Within the same millisecond it increments by a random amount (1 to 2^31), preserving both monotonicity and unpredictability.
Portable UUIDv7 generator for Clojure, ClojureScript, Babashka, nbb, and scittle. Implements RFC 9562 Section 6.2 Method 3 (monotonic random): - 48-bit millisecond Unix timestamp - 74-bit monotonically increasing random counter - Sub-millisecond ordering guaranteed from a single generator - No blocking, no spinning, no overflow in practice Usage: (require '[com.github.franks42.uuidv7.core :refer [uuidv7]]) (uuidv7) ;=> #uuid "0195xxxx-xxxx-7xxx-xxxx-xxxxxxxxxxxx" ;; Extract embedded data: (extract-ts u) ;=> 1738934578991 (ms since epoch) (extract-inst u) ;=> #inst "2025-02-07..." (as Date) (extract-key u) ;=> [ts a bh bl] (sortable composite key) The 74-bit counter space (~1.9 * 10^22 values per millisecond) is effectively inexhaustible. On each new millisecond the counter reseeds with fresh random bits. Within the same millisecond it increments by a random amount (1 to 2^31), preserving both monotonicity and unpredictability.
(extract-counter uuid)Extract the 74-bit monotonic counter from a UUIDv7 as a three-element vector [rand-a rand-b-hi rand-b-lo] (12 + 30 + 32 bits).
The vector compares lexicographically, preserving the same total order as the original UUID. Suitable as a composite key component: [(extract-ts u) (extract-counter u)]
Consistent shape on all platforms (JVM and JS).
Extract the 74-bit monotonic counter from a UUIDv7 as a three-element vector [rand-a rand-b-hi rand-b-lo] (12 + 30 + 32 bits). The vector compares lexicographically, preserving the same total order as the original UUID. Suitable as a composite key component: [(extract-ts u) (extract-counter u)] Consistent shape on all platforms (JVM and JS).
(extract-inst uuid)Extract the creation timestamp from a UUIDv7 as a Date/inst. Useful for logging, auditing, and debugging.
Extract the creation timestamp from a UUIDv7 as a Date/inst. Useful for logging, auditing, and debugging.
(extract-key uuid)Extract a sortable composite key [ts rand-a rand-b-hi rand-b-lo] from a UUIDv7.
The four-element vector compares lexicographically with the same total order as the original UUID. Useful when you want the (timestamp, counter) tuple as a map key or sort key without carrying the UUID itself.
Extract a sortable composite key [ts rand-a rand-b-hi rand-b-lo] from a UUIDv7. The four-element vector compares lexicographically with the same total order as the original UUID. Useful when you want the (timestamp, counter) tuple as a map key or sort key without carrying the UUID itself.
(extract-ts uuid)Extract the Unix epoch timestamp (milliseconds) from a UUIDv7. Works with any UUID type or UUID string.
Extract the Unix epoch timestamp (milliseconds) from a UUIDv7. Works with any UUID type or UUID string.
(make-generator)Create an independent UUIDv7 generator with its own monotonic state. Returns a zero-argument function that produces UUIDv7s.
Useful when you need multiple independent monotonic sequences, e.g. per-subsystem or per-thread dedicated generators.
Create an independent UUIDv7 generator with its own monotonic state. Returns a zero-argument function that produces UUIDv7s. Useful when you need multiple independent monotonic sequences, e.g. per-subsystem or per-thread dedicated generators.
(uuidv7)Generate a UUIDv7 with monotonic sub-millisecond ordering.
Returns java.util.UUID on JVM/BB, cljs.core/UUID on CLJS/nbb/scittle.
Successive calls from the same generator are guaranteed to produce strictly increasing UUIDs, even within the same millisecond.
Generate a UUIDv7 with monotonic sub-millisecond ordering. Returns java.util.UUID on JVM/BB, cljs.core/UUID on CLJS/nbb/scittle. Successive calls from the same generator are guaranteed to produce strictly increasing UUIDs, even within the same millisecond.
cljdoc 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 |