Created
September 15, 2023 09:18
-
-
Save borkdude/18b18232c00c2e2af2286d8bd36082d7 to your computer and use it in GitHub Desktop.
UUID v1 in babashka / Clojure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; based on code from https://www.baeldung.com/java-uuid | |
;; translated to Clojure by ChatGPT, minus the hex numbers which I evaluated in jshell | |
(defn get-64-least-significant-bits-for-version1 [] | |
(let [random (java.util.Random.) | |
random-63-bit-long (bit-and (.nextLong random) 4611686018427387903) | |
variant-3-bit-flag -9223372036854775808] | |
(bit-or random-63-bit-long variant-3-bit-flag))) | |
(defn get-64-most-significant-bits-for-version1 [] | |
(let [current-time-millis (System/currentTimeMillis) | |
time-low (bit-shift-left (bit-and current-time-millis 4294967295) 32) | |
time-mid (bit-shift-left (bit-and (bit-shift-right current-time-millis 32) 65535) 16) | |
version (bit-shift-left 1 12) | |
time-hi (bit-and (bit-shift-right current-time-millis 48) 4095)] | |
(bit-or time-low time-mid version time-hi))) | |
(defn generate-type1-uuid [] | |
(let [most-64-sig-bits (get-64-most-significant-bits-for-version1) | |
least-64-sig-bits (get-64-least-significant-bits-for-version1)] | |
(java.util.UUID. most-64-sig-bits least-64-sig-bits))) | |
(println (generate-type1-uuid)) ;; #uuid "98217366-018a-1000-b164-f4689455adbf" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made a Minecraft command out of your code here. Is it alright if I use it and publish it? It basically does the same as this tool.