Skip to content

Instantly share code, notes, and snippets.

@rodolfo42
Last active May 20, 2021 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodolfo42/f2ac54883e42ffc3bbae204662288a00 to your computer and use it in GitHub Desktop.
Save rodolfo42/f2ac54883e42ffc3bbae204662288a00 to your computer and use it in GitHub Desktop.
Crux & ZonedDateTime (vs. java.util.Date, LocalDate, LocalDateTime)
(ns crux-zoned-date-time
(:require [crux.api :as crux]
[clojure.pprint :refer [pprint]])
(:import [java.util UUID Date]
[java.time LocalDateTime ZonedDateTime LocalDate Instant]))
(def sample-n 10)
(defonce node
(crux/start-node {}))
(defonce tx
(crux/submit-tx node [[:crux.tx/put
{:crux.db/id (UUID/randomUUID)
:local-date (Date/from (Instant/parse "1990-01-01T00:00:00Z"))}]
[:crux.tx/put
{:crux.db/id (UUID/randomUUID)
:local-date (LocalDate/parse "1990-01-01")}]
[:crux.tx/put
{:crux.db/id (UUID/randomUUID)
:local-date-time (LocalDateTime/parse "1990-01-01T00:00:00")}]
[:crux.tx/put
{:crux.db/id (UUID/randomUUID)
:zoned-date-time (ZonedDateTime/parse "1990-01-01T00:00:00Z")}]]))
(crux.api/await-tx node tx)
(pprint
{:different-objects
{:java.util.Date
(doall (repeatedly sample-n #(crux/q (crux/db node)
'{:find [eid date-field]
:in [?date]
:where [[eid :date date-field]
[(> ?date date-field)]]}
[(Date/from (Instant/now))])))
:local-date
(doall (repeatedly sample-n #(crux/q (crux/db node)
'{:find [eid date-field]
:in [?date]
:where [[eid :local-date date-field]
[(> ?date date-field)]]}
[(LocalDate/now)])))
:local-date-time
(doall (repeatedly sample-n #(crux/q (crux/db node)
'{:find [eid date-field]
:in [?date]
:where [[eid :local-date-time date-field]
[(> ?date date-field)]]}
[(LocalDateTime/now)])))
:zoned-date-time
(doall (repeatedly sample-n #(crux/q (crux/db node)
'{:find [eid date-field]
:in [?date]
:where [[eid :zoned-date-time date-field]
[(> ?date date-field)]]}
[(ZonedDateTime/now)])))}})
(let [now-date (Date/from (Instant/now))
now-local-date (LocalDate/now)
now-local-date-time (LocalDateTime/now)
now-zoned-date-time (ZonedDateTime/now)]
(pprint
{:same-object
{:java.util.Date
(doall (repeatedly sample-n #(crux/q (crux/db node)
'{:find [eid date-field]
:in [?date]
:where [[eid :date date-field]
[(> ?date date-field)]]}
[now-date])))
:local-date
(doall (repeatedly sample-n #(crux/q (crux/db node)
'{:find [eid date-field]
:in [?date]
:where [[eid :local-date date-field]
[(> ?date date-field)]]}
[now-local-date])))
:local-date-time
(doall (repeatedly sample-n #(crux/q (crux/db node)
'{:find [eid date-field]
:in [?date]
:where [[eid :local-date-time date-field]
[(> ?date date-field)]]}
[now-local-date-time])))
:zoned-date-time
(doall (repeatedly sample-n #(crux/q (crux/db node)
'{:find [eid date-field]
:in [?date]
:where [[eid :zoned-date-time date-field]
[(> ?date date-field)]]}
[now-zoned-date-time])))}}))
{:deps {juxt/crux-core {:mvn/version "21.04-1.16.0-beta"}}}
{:different-objects
{:java.util.Date (#{} #{} #{} #{} #{} #{} #{} #{} #{} #{}),
:local-date (#{} #{} #{} #{} #{} #{} #{} #{} #{} #{}),
:local-date-time (#{} #{} #{} #{} #{} #{} #{} #{} #{} #{}),
:zoned-date-time
(#{[#uuid "80d65773-f4fa-4e7f-9501-8d4756bd4807"
#object[java.time.ZonedDateTime 0x427220d "1990-01-01T00:00Z"]]}
#{}
#{[#uuid "80d65773-f4fa-4e7f-9501-8d4756bd4807"
#object[java.time.ZonedDateTime 0x2cacf561 "1990-01-01T00:00Z"]]}
#{}
#{[#uuid "80d65773-f4fa-4e7f-9501-8d4756bd4807"
#object[java.time.ZonedDateTime 0x277e1946 "1990-01-01T00:00Z"]]}
#{}
#{[#uuid "80d65773-f4fa-4e7f-9501-8d4756bd4807"
#object[java.time.ZonedDateTime 0x337142ee "1990-01-01T00:00Z"]]}
#{}
#{}
#{[#uuid "80d65773-f4fa-4e7f-9501-8d4756bd4807"
#object[java.time.ZonedDateTime 0x49836032 "1990-01-01T00:00Z"]]})}}
{:same-object
{:java.util.Date (#{} #{} #{} #{} #{} #{} #{} #{} #{} #{}),
:local-date (#{} #{} #{} #{} #{} #{} #{} #{} #{} #{}),
:local-date-time (#{} #{} #{} #{} #{} #{} #{} #{} #{} #{}),
:zoned-date-time
(#{[#uuid "80d65773-f4fa-4e7f-9501-8d4756bd4807"
#object[java.time.ZonedDateTime 0x45abb13d "1990-01-01T00:00Z"]]}
#{[#uuid "80d65773-f4fa-4e7f-9501-8d4756bd4807"
#object[java.time.ZonedDateTime 0x73a7a027 "1990-01-01T00:00Z"]]}
#{[#uuid "80d65773-f4fa-4e7f-9501-8d4756bd4807"
#object[java.time.ZonedDateTime 0xa22905 "1990-01-01T00:00Z"]]}
#{[#uuid "80d65773-f4fa-4e7f-9501-8d4756bd4807"
#object[java.time.ZonedDateTime 0xbf24906 "1990-01-01T00:00Z"]]}
#{[#uuid "80d65773-f4fa-4e7f-9501-8d4756bd4807"
#object[java.time.ZonedDateTime 0x1095f06 "1990-01-01T00:00Z"]]}
#{[#uuid "80d65773-f4fa-4e7f-9501-8d4756bd4807"
#object[java.time.ZonedDateTime 0x2ab338cf "1990-01-01T00:00Z"]]}
#{[#uuid "80d65773-f4fa-4e7f-9501-8d4756bd4807"
#object[java.time.ZonedDateTime 0x59e04b17 "1990-01-01T00:00Z"]]}
#{[#uuid "80d65773-f4fa-4e7f-9501-8d4756bd4807"
#object[java.time.ZonedDateTime 0x519d9dcb "1990-01-01T00:00Z"]]}
#{[#uuid "80d65773-f4fa-4e7f-9501-8d4756bd4807"
#object[java.time.ZonedDateTime 0x4661fe61 "1990-01-01T00:00Z"]]}
#{[#uuid "80d65773-f4fa-4e7f-9501-8d4756bd4807"
#object[java.time.ZonedDateTime 0x1267fe8b "1990-01-01T00:00Z"]]})}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment