Skip to content

Commit

Permalink
ms argument to squuid (closes #95, #97)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Jul 19, 2015
1 parent 6e2ae22 commit f2f05fa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# WIP

- msec argument to squuid (#95, #97)

# 0.11.5

- Ported BTSet to JVM
Expand Down
12 changes: 8 additions & 4 deletions src/datascript.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,14 @@
(< c l) (str (apply str (repeat (- l c) "0")) s)
:else s))))

(defn squuid []
(defn squuid
([]
(squuid #?(:clj (System/currentTimeMillis)
:cljs (.getTime (js/Date.)))))
([msec]
#?(:clj
(let [uuid (UUID/randomUUID)
time (int (/ (System/currentTimeMillis) 1000))
time (int (/ msec 1000))
high (.getMostSignificantBits uuid)
low (.getLeastSignificantBits uuid)
new-high (bit-or (bit-and high 0x00000000FFFFFFFF)
Expand All @@ -207,14 +211,14 @@
:cljs
(uuid
(str
(-> (int (/ (.getTime (js/Date.)) 1000))
(-> (int (/ msec 1000))
(to-hex-string 8))
"-" (-> (rand-bits 16) (to-hex-string 4))
"-" (-> (rand-bits 16) (bit-and 0x0FFF) (bit-or 0x4000) (to-hex-string 4))
"-" (-> (rand-bits 16) (bit-and 0x3FFF) (bit-or 0x8000) (to-hex-string 4))
"-" (-> (rand-bits 16) (to-hex-string 4))
(-> (rand-bits 16) (to-hex-string 4))
(-> (rand-bits 16) (to-hex-string 4))))))
(-> (rand-bits 16) (to-hex-string 4)))))))

(defn squuid-time-millis [uuid]
#?(:clj (-> (.getMostSignificantBits ^UUID uuid)
Expand Down

0 comments on commit f2f05fa

Please sign in to comment.