forked from nalgeon/uuidv7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuidv7.el
28 lines (22 loc) · 839 Bytes
/
uuidv7.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(require 'cl-lib)
(defun uuidv7 ()
"Generates an array representing the bytes of an UUIDv7 label."
(let* ((timestamp (car (time-convert (current-time) 1000)))
(timestamp-bytes
(cl-loop for i from 5 downto 0
collect (logand (ash timestamp (* i -8)) #xFF)))
(uuid (make-vector 16 0)))
(cl-loop for i below 16 do
(aset uuid i
(if (< i 6)
(nth i timestamp-bytes)
(random 256))))
(aset uuid 6 (logior (logand (elt uuid 6) #x0F) #x70))
(aset uuid 8 (logior (logand (elt uuid 8) #x3F) #x80))
uuid))
(defun bytes-to-hexstring (bytes)
"Converts a vector of bytes into a hexadecimal string."
(cl-loop for byte across bytes
concat (format "%02x" byte)))
(let ((uuid-bytes (uuidv7)))
(message "%s" (bytes-to-hexstring uuid-bytes)))