This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-08-15
Channels
- # architecture (5)
- # babashka (34)
- # beginners (72)
- # calva (42)
- # cherry (31)
- # cider (14)
- # clojure (27)
- # clojure-europe (11)
- # clojure-norway (17)
- # clojure-uk (1)
- # clojurescript (25)
- # community-development (13)
- # conjure (1)
- # core-async (11)
- # datascript (18)
- # datomic (11)
- # emacs (12)
- # fulcro (10)
- # integrant (5)
- # introduce-yourself (3)
- # jobs (8)
- # juxt (2)
- # malli (22)
- # off-topic (11)
- # pathom (18)
- # polylith (62)
- # rdf (18)
- # reagent (8)
- # releases (1)
- # shadow-cljs (35)
- # sql (3)
- # squint (141)
- # tools-deps (12)
- # vim (4)
- # xtdb (4)
I had the same question, I came up with these two versions (I'm not certain they are completely correct):
(defn within?
"Is date inside given interval (or start end)"
([intvl d]
(contains? #{:starts :meets :during :finishes}
(t.i/relation d intvl)))
([start end d]
(within? (t.i/new-interval start end) d)))
(defn within2?
"Is date inside given interval (or start end)"
([intvl d]
(t/< (->date-time (t/beginning intvl)) (->date-time d) (->date-time (t/end intvl))))
([start end d]
(t/< (->date-time start) (->date-time d) (->date-time end))))
->date-time
takes any date/time object and casts to a date-time so you can just always pass a date-time instead and remove thatfor -> js/Date
there is t/inst
again a more general cast fn:
(defn ->inst [d]
(cond
(date? d) (t/inst (t/at d (t/midnight)))
(date-time? d) (t/inst d)
(instant? d) (t/inst d)
(time? d) (t/at (t/today) d)
(inst? d) d
(integer? d) (t/instant d)
:else (throw (error "Cannot convert " (pr-str d) " to inst."))))
Note that per your domain this may not be the logic you want as dates are based on calendars, and instants are on the Universal Timeline.