Skip to content

Instantly share code, notes, and snippets.

@pbalduino
Last active September 19, 2019 18:46
Show Gist options
  • Save pbalduino/c9943b678d5d8e56542ccfcde0c0895d to your computer and use it in GitHub Desktop.
Save pbalduino/c9943b678d5d8e56542ccfcde0c0895d to your computer and use it in GitHub Desktop.
(ns generate-dates
(:require [clojure.spec.alpha :as s]
[clojure.spec.gen.alpha :as gen]))
(def positive-long (s/and integer? pos?))
(s/def ::timestamp
(s/with-gen
positive-long
#(gen/fmap (fn [number] (- (inst-ms (java.time.Instant/now)) number)) (s/gen positive-long))))
(gen/generate (s/gen ::timestamp))
; Instead of java,util.Date, you can use whatever library/class you want/need.
(map #(java.util.Date. %) (gen/sample (s/gen ::timestamp) 10))
; (#inst "2019-09-19T18:45:06.399-00:00" #inst "2019-09-19T18:45:06.358-00:00" #inst "2019-09-19T18:45:06.438-00:00" #inst "2019-09-19T18:45:06.434-00:00" #inst "2019-09-19T18:45:06.427-00:00" #inst "2019-09-19T18:45:06.427-00:00" #inst "2019-09-19T18:45:06.417-00:00" #inst "2019-09-19T18:45:06.417-00:00" #inst "2019-09-19T18:45:06.441-00:00" #inst "2019-09-19T18:45:06.344-00:00")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment