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 :as uuidv7]) (uuidv7/uuidv7) ;=> #uuid "0195xxxx-xxxx-7xxx-xxxx-xxxxxxxxxxxx"
;; Extract embedded data: (uuidv7/extract-ts u) ;=> 1738934578991 (ms since epoch) (uuidv7/extract-inst u) ;=> #inst "2025-02-07..." (as Date) (uuidv7/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.
The extraction functions (extract-ts, extract-counter, extract-key,
extract-inst) require a UUIDv7. Use uuidv7? to validate first:
(when (uuidv7/uuidv7? u) (uuidv7/extract-ts u)) ;=> Safe to call after validation
Passing a non-v7 UUID to an extraction function will throw an AssertionError.
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 :as uuidv7])
(uuidv7/uuidv7) ;=> #uuid "0195xxxx-xxxx-7xxx-xxxx-xxxxxxxxxxxx"
;; Extract embedded data:
(uuidv7/extract-ts u) ;=> 1738934578991 (ms since epoch)
(uuidv7/extract-inst u) ;=> #inst "2025-02-07..." (as Date)
(uuidv7/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.
## UUID Validation
The extraction functions (`extract-ts`, `extract-counter`, `extract-key`,
`extract-inst`) require a UUIDv7. Use `uuidv7?` to validate first:
(when (uuidv7/uuidv7? u)
(uuidv7/extract-ts u)) ;=> Safe to call after validation
Passing a non-v7 UUID to an extraction function will throw an AssertionError.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 |