Back
instant (clj)
(source)function
(instant)
(instant v)
Examples
tick
(ns tick.api-test
(:require
[clojure.test
:refer [deftest is testing run-tests]
:refer-macros [deftest is testing run-tests]]
[tick.core :as t]
[tick.locale-en-us]
[tick.protocols :as p]
[cljc.java-time.clock]
[cljc.java-time.instant]
[cljc.java-time.day-of-week]
[cljc.java-time.month]
[cljc.java-time.year]
#?@(:cljs [[java.time :refer [Instant]]]))
#?(:clj
(:import [java.time Instant])))
(deftest date-construction-test
(is (= (t/date "2018-01-11")
(t/date (t/instant 1515691416624))))
(is (t/date-time? (t/noon (t/today))))
(t/with-clock (-> (t/date "2018-02-14") (t/at "10:00"))
(testing "(noon (today))"
(is (= "2018-02-14T12:00" (str (t/noon (t/today))))))
(testing "(noon (date))"
(is (= "2018-02-14T12:00" (str (t/noon (t/date))))))))
(deftest clock-test
(testing "clock"
(t/with-clock (-> (t/date "2018-02-14") (t/at "10:00") (t/in "America/New_York"))
(testing "(clock) return type"
(is (t/clock? (t/clock))))
(testing "Time shifting the clock back by 2 hours"
(is (= "2018-02-14T13:00:00Z" (str (t/instant (t/<< (t/clock) (t/new-duration 2 :hours)))))))
(testing "with instant"
(is (= (t/zone (t/clock (t/instant)))
(t/zone "America/New_York"))))))
(testing "Creation of clock with fixed instant"
(is (= "2017-10-31T16:00:00Z" (str (t/instant (t/clock (t/instant "2017-10-31T16:00:00Z")))))))
(deftest constructor-test
(is (t/year? (t/year 2017)))
(is (= 2017 (cljc.java-time.year/get-value (t/year 2017))))
(is (t/month? (t/month 12)))
(is (= t/DECEMBER (t/month 12)))
(is (= (t/new-date 3030 3 3)
(t/date "3030-03-03")))
(is (-> (t/new-duration 1000 :millis)
(t/inst)
(t/instant)
(cljc.java-time.instant/to-epoch-milli)
(= 1000)))
(is (= (t/new-year-month 2020 7)
(t/year-month "2020-07"))))
;; Point-in-time tests
(deftest today-test
(t/with-clock (cljc.java-time.clock/fixed (t/instant "2017-08-08T12:00:00Z") t/UTC)
(is (= (t/instant "2017-08-08T12:00:00Z") (t/now)))
(is (= (t/date "2017-08-08") (t/today)))
(is (= (t/date "2017-08-07") (t/yesterday)))
(is (= (t/date "2017-08-09") (t/tomorrow)))
(is (= 8 (t/day-of-month (t/today))))
(is (= 2017 (t/int (t/year))))
(is (= (t/date-time "2017-08-08T12:00:00") (t/noon (t/today))))
(is (= (t/date-time "2017-08-08T00:00:00") (t/midnight (t/today))))))
(deftest instant-test
(testing "instant basics"
(is (t/instant? (t/instant (t/now))))
(is (t/instant? (t/instant (str cljc.java-time.instant/min))))
(is (t/instant? (t/instant (t/zoned-date-time))))))
(deftest epoch-test
(is (= (cljc.java-time.instant/parse "1970-01-01T00:00:00Z") (t/epoch))))
(let [now (t/instant)
from (t/<< now (t/new-duration 10 :seconds))
to (t/>> now (t/new-duration 10 :seconds))]
(is (= (t/new-duration 20 :seconds) (t/between from to) ))
(is (= 20 (t/between from to :seconds))))
(is
(= (t/new-duration 48 :hours)
(t/between (t/beginning (t/today)) (t/end (t/tomorrow)))))
(let [start (t/date-time "2020-01-01T12:00")
end (t/date-time "2020-01-01T12:02")]
(is
(=
(t/new-duration 2 :minutes)
(t/between start end))
(= 2 (t/between start end :minutes))))
(is
(=
(t/new-duration 30 :minutes)
(t/between (t/new-time 11 0 0) (t/new-time 11 30 0))))
(testing "LocalDate"
(let [start (t/date "2020-01-01")
end (t/date "2020-01-02")]
(is (= (t/new-period 1 :days)
(t/between start end)))
(is (= 1 (t/between start end :days))))))
(deftest truncate-test
(let [dates [(t/instant) (t/zoned-date-time) (t/date-time)
(t/offset-date-time) (t/time)]
truncate-tos [:nanos
:micros
:millis
:seconds
:minutes
:hours
:half-days
:days ]]
(doseq [date dates
truncate-to truncate-tos]
(is (t/truncate date truncate-to)))))
(deftest comparison-test
(let [point (t/truncate (t/instant) :millis)
later (t/>> point (t/new-duration 1 :millis))]
(testing "shifting inst"
(let [i (t/inst)]
(is (= i (-> i
(t/>> (t/new-duration 10 :seconds))
(t/<< (t/new-duration 10 :seconds)))))))
(testing "max-min"
(is (= later (t/max point later point later)))
(is (= point (t/min point later point later))))
(testing "max-min key"
(is (= {:foo later} (t/max-key :foo {:foo point} {:foo later} {:foo point} {:foo later})))
(is (= {:foo point} (t/min-key :foo {:foo point} {:foo later} {:foo point} {:foo later}))))
(testing "comparables not="
(doseq [point (point-in-time-comparable point)]
(testing "comparables ="
(is (apply t/= point (point-in-time-comparable point)))
(is (apply t/>= point (point-in-time-comparable point))))
(is (apply t/<= point (point-in-time-comparable later))))
(doseq [later (point-in-time-comparable later)]
(is (apply t/>= later (point-in-time-comparable point))))
(testing "Instants and ZonedDateTimes should be equals if represents the same point in time"
(is (t/=
(t/instant (t/clock (t/instant "2017-10-31T16:00:00Z")))
(t/zoned-date-time "2017-10-31T16:00:00Z[UTC]"))))
(is
(t/<
(t/now)
(t/>> (t/now) (t/new-duration 10 :seconds))
(t/>> (t/now) (t/new-duration 20 :seconds))))
(is
(t/>
(t/>> (t/now) (t/new-duration 20 :seconds))
(t/>> (t/now) (t/new-duration 10 :seconds))
(t/now)))
(is (not
(t/<
(t/now)
(t/>> (t/now) (t/new-duration 20 :seconds))
(t/>> (t/now) (t/new-duration 10 :seconds)))))
(let [at (t/now)]
(is (t/<= at at (t/>> at (t/new-duration 1 :seconds))))
(is (t/>= at at (t/<< at (t/new-duration 10 :seconds)))))
(deftest coincidence-test
(let [int-beginning (t/instant "2020-02-02T00:00:00Z")
int-end (t/>> int-beginning (t/of-hours 2))
interval {:tick/beginning int-beginning
:tick/end int-end}]
(is (t/coincident? interval (t/>> int-beginning (t/of-hours 1))))
(is (not (t/coincident? interval (t/<< int-beginning (t/of-hours 1)))))
(is (t/coincident? interval int-beginning))
(is (t/coincident? interval int-end))
(is (t/coincident? interval interval))
(is (not (t/coincident? interval (-> interval
(update :tick/end #(t/>> % (t/of-nanos 1))))))))
(testing "non-interval coincidence"
(doseq [[start-f new-amount] [[t/date t/of-days] [t/date-time t/of-hours]]]
(let [start (start-f)
end (t/>> start (new-amount 2))]
(is (t/coincident? start end (t/>> start (new-amount 1))))
(is (not (t/coincident? start end (t/<< start (new-amount 1)))))
(is (t/coincident? start end start))
(is (t/coincident? start end end))))))
(deftest predicates-test
(is (true? (t/clock? (t/clock))))
(is (true? (t/day-of-week? t/MONDAY)))
(is (true? (t/duration? (t/new-duration 1 :minutes))))
(is (true? (t/instant? (t/instant))))
(is (true? (t/date? (t/today))))
(is (true? (t/date-time? (t/at (t/today) (t/new-time 0 0)))))
(is (true? (t/time? (t/new-time 0 0))))
(is (true? (t/month? t/MAY)))
(is (true? (t/offset-date-time? (t/offset-date-time))))
(is (true? (t/period? (t/new-period 1 :weeks))))
(is (true? (t/year? (t/year))))
(is (true? (t/year-month? (t/year-month))))
(is (true? (t/zone? (t/zone))))
(is (true? (t/zone-offset? (t/zone-offset (t/zoned-date-time)))))
(is (true? (t/zoned-date-time? (t/zoned-date-time))))
(is (false? (t/date? 16)))
(is (false? (t/month? 16))))
(deftest in-test
(is (= (t/zoned-date-time "2021-04-23T11:23:24.576270-04:00[America/Toronto]")
(t/in (t/instant "2021-04-23T15:23:24.576270Z")
(t/zone "America/Toronto"))))
(is (= (t/zoned-date-time "2021-04-23T11:18:46.594720-04:00[America/Toronto]")
(t/in (t/offset-date-time "2021-04-23T13:18:46.594720-02:00")
(t/zone "America/Toronto"))))
(is (= (t/zoned-date-time "2021-04-23T11:18:46.594720-04:00[America/Toronto]")
(t/in (t/zoned-date-time "2021-04-23T08:18:46.594720-07:00[America/Los_Angeles]")
(t/zone "America/Toronto")))))
tick
(ns tick.alpha.interval-test
(:require
[clojure.spec.alpha :as s]
[tick.core :as t]
[tick.protocols :as p]
[clojure.test
:refer [deftest is testing run-tests]
:refer-macros [deftest is testing run-tests]]
[tick.alpha.interval :as ti]
#?@(:cljs [[java.time :refer [Instant LocalDateTime LocalTime]]]))
#?(:clj
(:import [java.time LocalDateTime Instant LocalTime])))
;; We can construct every possible combination of interval relation with just 4 instants.
(def instants [(t/instant "2017-07-30T09:00:00Z")
(t/instant "2017-07-30T11:00:00Z")
(t/instant "2017-07-30T13:00:00Z")
(t/instant "2017-07-30T15:00:00Z")])
;; Distinct: because no pair of definite intervals can be related by more than one of the relationships.
;; From [ALSPAUGH-2009]
(deftest distinct-test
(is
(= [1] ; Each interval should have just one relation that is true
(distinct
(let [f (apply juxt ti/basic-relations)]
(for [x1 instants
x2 instants
y1 instants
y2 instants
:when (t/< x1 x2)
:when (t/< y1 y2)
:let [x (ti/new-interval x1 x2)
y (ti/new-interval y1 y2)]]
;; For each combination, count how many relations are true
;; (should be just one each time)
(count (filter true? (f x y)))))))))
;; Exhaustive: because any pair of definite intervals are described by one of the relations.
(deftest exhaustive-test []
(is
(= 13 ; Thirteen basic relations
(count
(distinct
(for [x1 instants
x2 instants
y1 instants
y2 instants
:when (t/< x1 x2)
:when (t/< y1 y2)
:let [x (ti/new-interval x1 x2)
y (ti/new-interval y1 y2)]]
;; For each combination, count how many relations are true
;; (should be just one each time)
(ti/relation x y)))))))
(deftest disjoint-test []
(is (ti/disjoint?
(ti/new-interval (instants 0) (instants 1))
(ti/new-interval (instants 2) (instants 3))))
(is (= (ti/disjoint?
(ti/new-interval (instants 0) (instants 1))
(ti/new-interval (instants 2) (instants 3))) ti/precedes?))
(is (nil?
(ti/disjoint?
(ti/new-interval (instants 0) (instants 2))
(ti/new-interval (instants 1) (instants 3)))))
(is (nil?
(ti/disjoint?
(ti/new-interval (instants 0) (instants 3))
(ti/new-interval (instants 1) (instants 2))))))
(deftest concur?-test []
(is (nil?
(ti/concur?
(ti/new-interval (instants 0) (instants 1))
(ti/new-interval (instants 2) (instants 3)))))
(is (= (ti/concur?
(ti/new-interval (instants 0) (instants 2))
(ti/new-interval (instants 1) (instants 3)))
ti/overlaps?))
(is (= (ti/concur?
(ti/new-interval (instants 0) (instants 3))
(ti/new-interval (instants 1) (instants 2)))
ti/contains?)))
(deftest concur-test []
(is
(=
(ti/new-interval (instants 1) (instants 2))
(ti/concur
(ti/new-interval (instants 0) (instants 2))
(ti/new-interval (instants 1) (instants 3)))))
(is
(=
(ti/new-interval (instants 1) (instants 2))
(ti/concur
(ti/new-interval (instants 1) (instants 3))
(ti/new-interval (instants 0) (instants 2)))))
(is
(nil?
(ti/concur
(ti/new-interval (instants 0) (instants 1))
(ti/new-interval (instants 2) (instants 3)))))
(is
(nil?
(ti/concur
(ti/new-interval (instants 0) (instants 1))
(ti/new-interval (instants 1) (instants 2)))))
(is
(=
(ti/new-interval (instants 0) (instants 2))
(ti/concur
(ti/new-interval (instants 0) (instants 2))
(ti/new-interval (instants 0) (instants 3)))))
(is
(=
(ti/new-interval (instants 0) (instants 2))
(ti/concur
(ti/new-interval (instants 0) (instants 3))
(ti/new-interval (instants 0) (instants 2)))))
(is
(=
(ti/new-interval (instants 1) (instants 3))
(ti/concur
(ti/new-interval (instants 1) (instants 3))
(ti/new-interval (instants 0) (instants 3)))))
(is
(=
(ti/new-interval (instants 1) (instants 2))
(ti/concur
(ti/new-interval (instants 1) (instants 3))
(ti/new-interval (instants 1) (instants 2))
(ti/new-interval (instants 0) (instants 2)))))
(is
(nil?
(ti/concur
(ti/new-interval (instants 1) (instants 2))
(ti/new-interval (instants 2) (instants 3))
(ti/new-interval (instants 0) (instants 2)))))
(is
(nil?
(ti/concur
(ti/new-interval (instants 0) (instants 1))
(ti/new-interval (instants 1) (instants 2))
(ti/new-interval (instants 2) (instants 3))))))
(deftest ordered-disjoint-intervals?-test
(is
(ti/ordered-disjoint-intervals? []))
(is
(ti/ordered-disjoint-intervals?
[(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
(t/instant "2017-07-30T10:00:00Z"))]))
(is
(ti/ordered-disjoint-intervals?
[(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
(t/instant "2017-07-30T10:00:00Z"))
(ti/new-interval (t/instant "2017-07-30T11:00:00Z")
(t/instant "2017-07-30T13:00:00Z"))]))
(is
(ti/ordered-disjoint-intervals?
[(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
(t/instant "2017-07-30T11:00:00Z"))
(ti/new-interval (t/instant "2017-07-30T11:00:00Z")
(t/instant "2017-07-30T13:00:00Z"))]))
(is
(ti/ordered-disjoint-intervals?
[(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
(t/instant "2017-07-30T11:00:00Z"))
(ti/new-interval (t/instant "2017-07-30T11:00:00Z")
(t/instant "2017-07-30T13:00:00Z"))
(ti/new-interval (t/instant "2017-07-30T16:00:00Z")
(t/instant "2017-07-30T18:00:00Z"))]))
(is
(false?
(ti/ordered-disjoint-intervals?
[(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
(t/instant "2017-07-30T12:00:00Z"))
(ti/new-interval (t/instant "2017-07-30T11:00:00Z")
(t/instant "2017-07-30T13:00:00Z"))])))
(is
(false?
(ti/ordered-disjoint-intervals?
[(ti/new-interval (t/instant "2017-07-30T11:00:00Z")
(t/instant "2017-07-30T13:00:00Z"))
(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
(t/instant "2017-07-30T10:00:00Z"))]))))
(deftest union-test
(testing "counts"
(let [coll1 [(ti/new-interval (t/instant "2017-07-30T09:00:00Z")
(t/instant "2017-07-30T12:00:00Z"))]
coll2 [(ti/new-interval (t/instant "2017-07-30T11:00:00Z")
(t/instant "2017-07-30T15:00:00Z"))]
coll3 [(ti/new-interval (t/instant "2017-07-30T17:00:00Z")
(t/instant "2017-07-30T19:00:00Z"))]]
(is (= 1 (count (ti/union coll1 coll2))))
(is (ti/ordered-disjoint-intervals? (ti/union coll1 coll2)))
(is (= 2 (count (ti/union coll1 coll2 coll3))))
(is (ti/ordered-disjoint-intervals? (ti/union coll1 coll2 coll3)))))
(testing "union"
(let [ival1 (ti/new-interval (t/instant "2017-07-30T09:00:00Z")
(t/instant "2017-07-30T10:00:00Z"))
ival2 (ti/new-interval (t/instant "2017-07-30T10:00:00Z")
(t/instant "2017-07-30T11:00:00Z"))
ival3 (ti/new-interval (t/instant "2017-07-30T11:00:00Z")
(t/instant "2017-07-30T12:00:00Z"))
ival4 (ti/new-interval (t/instant "2017-07-30T12:00:00Z")
(t/instant "2017-07-30T13:00:00Z"))
ival5 (ti/new-interval (t/instant "2017-07-30T13:00:00Z")
(t/instant "2017-07-30T14:00:00Z"))
res (ti/union [ival2 ival4] [ival1 ival3 ival5])]
(is (= res [ival1 ival2 ival3 ival4 ival5])))))
(deftest intersection-test
(let [coll1 [(ti/new-interval (t/instant "2017-01-01T06:00:00Z")
(t/instant "2017-01-01T07:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T09:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T09:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T13:00:00Z")
(t/instant "2017-01-01T15:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T17:00:00Z")
(t/instant "2017-01-01T19:00:00Z"))]
coll2 [(ti/new-interval (t/instant "2017-01-01T09:00:00Z")
(t/instant "2017-01-01T10:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T11:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T14:00:00Z")
(t/instant "2017-01-01T18:00:00Z"))]]
(is
(= [(ti/new-interval (t/instant "2017-01-01T09:00:00Z") (t/instant "2017-01-01T10:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T11:00:00Z") (t/instant "2017-01-01T12:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T14:00:00Z") (t/instant "2017-01-01T15:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T17:00:00Z") (t/instant "2017-01-01T18:00:00Z"))]
(ti/intersection coll1 coll2))))
(let [coll1 [(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T14:00:00Z")
(t/instant "2017-01-01T16:00:00Z"))]
coll2 [(ti/new-interval (t/instant "2017-01-01T09:00:00Z")
(t/instant "2017-01-01T11:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T13:00:00Z")
(t/instant "2017-01-01T17:00:00Z"))]]
(is
(= [(ti/new-interval (t/instant "2017-01-01T09:00:00Z") (t/instant "2017-01-01T11:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T14:00:00Z") (t/instant "2017-01-01T16:00:00Z"))]
(ti/intersection coll1 coll2))))
(let [coll1 [(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T14:00:00Z")
(t/instant "2017-01-01T16:00:00Z"))]
coll2 [(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))]]
(is
(=
[(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))]
(ti/intersection coll1 coll2))))
(let [coll1 [(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T17:00:00Z")
(t/instant "2017-01-01T19:00:00Z"))]
coll2 [(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T18:00:00Z"))]]
(is (=
[(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T17:00:00Z")
(t/instant "2017-01-01T18:00:00Z"))]
(ti/intersection coll1 coll2))))
(let [coll1 [(ti/new-interval (t/instant "2017-01-01T12:00:00Z")
(t/instant "2017-01-01T14:00:00Z"))]
coll2 [(ti/new-interval (t/instant "2017-01-01T11:00:00Z")
(t/instant "2017-01-01T14:00:00Z"))]]
(is (= [(ti/new-interval (t/instant "2017-01-01T12:00:00Z")
(t/instant "2017-01-01T14:00:00Z"))]
(ti/intersection coll1 coll2))))
(testing "Empty sets"
(let [coll1 []
coll2 [(ti/new-interval (t/instant "2017-01-01T09:00:00Z")
(t/instant "2017-01-01T10:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T11:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T14:00:00Z")
(t/instant "2017-01-01T18:00:00Z"))]]
(is
(empty? (ti/intersection coll1 coll2)))
(is
(empty? (ti/intersection coll2 coll1)))
(is
(empty? (ti/intersection [] [])))))))
(deftest difference-test
(let [coll1 [(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T14:00:00Z")
(t/instant "2017-01-01T16:00:00Z"))]
coll2 [(ti/new-interval (t/instant "2017-01-01T09:00:00Z")
(t/instant "2017-01-01T11:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T13:00:00Z")
(t/instant "2017-01-01T17:00:00Z"))]]
(is
(= [(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T09:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T11:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))]
(ti/difference coll1 coll2))))
(let [coll1 [(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T14:00:00Z")
(t/instant "2017-01-01T16:00:00Z"))]
coll2 [(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))]]
(is
(=
[(ti/new-interval (t/instant "2017-01-01T14:00:00Z")
(t/instant "2017-01-01T16:00:00Z"))]
(ti/difference coll1 coll2))))
(let [coll1 [(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T17:00:00Z")
(t/instant "2017-01-01T19:00:00Z"))]
coll2 [(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T18:00:00Z"))]]
(is (=
[(ti/new-interval (t/instant "2017-01-01T18:00:00Z")
(t/instant "2017-01-01T19:00:00Z"))]
(ti/difference coll1 coll2))))
(let [coll1 [(ti/new-interval (t/instant "2017-01-01T12:00:00Z")
(t/instant "2017-01-01T14:00:00Z"))]
coll2 [(ti/new-interval (t/instant "2017-01-01T11:00:00Z")
(t/instant "2017-01-01T14:00:00Z"))]]
(is (empty? (ti/difference coll1 coll2))))
(testing "Empty sets"
(let [coll1 []
coll2 [(ti/new-interval (t/instant "2017-01-01T09:00:00Z")
(t/instant "2017-01-01T10:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T11:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T14:00:00Z")
(t/instant "2017-01-01T18:00:00Z"))]]
(is
(= []
(ti/difference coll1 coll2)))
(is
(= coll2
(ti/difference coll2 coll1)))
(is
(= []
(ti/difference [] []))))))
(deftest difference-invariant-test
(let [coll1 [(ti/new-interval (t/instant "2017-01-01T14:00:00Z")
(t/instant "2017-01-01T16:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T08:00:00Z")
(t/instant "2017-01-01T12:00:00Z"))]
coll2 [(ti/new-interval (t/instant "2017-01-01T09:00:00Z")
(t/instant "2017-01-01T11:00:00Z"))
(ti/new-interval (t/instant "2017-01-01T13:00:00Z")
(t/instant "2017-01-01T17:00:00Z"))]]
(is
(thrown?
#?(:clj clojure.lang.ExceptionInfo
:cljs ExceptionInfo)
(ti/difference coll1 coll2)))))
(deftest am-test
(t/with-clock (cljc.java-time.clock/fixed (t/instant "2017-08-08T12:00:00Z") t/UTC)
(is (= (ti/new-interval (t/date-time "2017-08-08T00:00:00")
(t/date-time "2017-08-08T12:00:00"))
(ti/am (t/today))))
(is (= (ti/new-interval (t/date-time "2017-08-08T12:00:00")
(t/date-time "2017-08-09T00:00:00"))
(ti/pm (t/today))))))
;; TODO: Think about conversions between single instants and intervals. Feather? Widen? Smudge?
lumberdev/tesserae
(ns tesserae.serialize
(:require [tick.core :as t]
[time-literals.read-write]
[cognitect.transit :as transit]
[hyperfiddle.electric :as e]
#?(:cljs
[java.time :refer [Period
LocalDate
LocalDateTime
ZonedDateTime
OffsetTime
Instant
OffsetDateTime
ZoneId
DayOfWeek
LocalTime
Month
Duration
Year
YearMonth]]))
#?(:clj (:import
(java.time Period
LocalDate
LocalDateTime
ZonedDateTime
OffsetTime
Instant
OffsetDateTime
ZoneId
DayOfWeek
LocalTime
Month
Duration
Year
YearMonth))))
(def time-classes
{'time/period Period
'time/date LocalDate
'time/date-time LocalDateTime
'time/zoned-date-time ZonedDateTime
'time/instant Instant
;;'offset-time OffsetTime
;;'offset-date-time OffsetDateTime
'time/time LocalTime
'time/duration Duration
'time/year Year
'time/year-month YearMonth
'time/zone ZoneId
'time/day-of-week DayOfWeek
'time/month Month})
borgeby/jarl
(ns jarl.builtins.http-test
(:require [jarl.builtins.http :refer [builtin-http-send content-type create-request parse-timeout]]
[jarl.exceptions :as errors]
[jarl.types :refer [rego-equal?]]
[jarl.http-client :as http-client]
[jarl.time :as time]
#?(:clj [clojure.test :refer [deftest is testing]]
:cljs [cljs.test :refer [deftest is testing]])
[tick.core :as t]))
(testing "force cache duration"
(let [counter (atom 0)
req {"method" "get" "url" "http://borge.by/bar" "force_cache" true "force_cache_duration_seconds" 30}]
(with-redefs [http-client/send-request
(fn [_]
(swap! counter inc)
{:status 200 :headers {"content-type" "application/json"} :body "{\"foo\":\"bar\"}"})]
(let [res {"body" {"foo" "bar"} "headers" {"content-type" "application/json"}
"raw_body" "{\"foo\":\"bar\"}" "status" "200 OK" "status_code" 200}
now (time/now-ns)
exp (time/instant->ns (t/>> (t/now) (t/new-duration 60 :seconds)))
bctx {:time-now-ns now}]
(is (= (builtin-http-send {:args [req] :builtin-context bctx}) res))
(is (= @counter 1))
(is (= (builtin-http-send {:args [req] :builtin-context bctx}) res))
(is (= @counter 1))
(is (= (builtin-http-send {:args [req] :builtin-context {:time-now-ns exp}}) res)) ; fast-forward 1 minute
(is (= @counter 2))))))
(testing "cache from response headers, cache-control"
(let [counter (atom 0)
req {"method" "get" "url" "http://borge.by" "cache" true}]
(with-redefs [http-client/send-request
(fn [_]
(swap! counter inc)
{:status 200
; note upper-case keys
:headers {"Content-Type" "application/json"
"Cache-Control" "ignored, max-age=30, also=ignored"
"Date" "Sat, 01 Jan 2022 15:10:05 GMT"}
:body "{\"foo\":\"bar\"}"})]
(let [res {"body" {"foo" "bar"}
"headers" {"content-type" "application/json"
"cache-control" "ignored, max-age=30, also=ignored"
"date" "Sat, 01 Jan 2022 15:10:05 GMT"}
"raw_body" "{\"foo\":\"bar\"}" "status" "200 OK" "status_code" 200}
now (t/instant (t/zoned-date-time "2022-01-01T15:10:05Z"))
exp (time/instant->ns (t/>> now (t/new-duration 60 :seconds)))
bctx {:time-now-ns (time/instant->ns now)}]
(is (= (builtin-http-send {:args [req] :builtin-context bctx}) res))
(is (= @counter 1))
(is (= (builtin-http-send {:args [req] :builtin-context bctx}) res))
(is (= @counter 1))
(is (= (builtin-http-send {:args [req] :builtin-context {:time-now-ns exp}}) res)) ; fast-forward 1 minute
(is (= @counter 2))))))
(testing "cache from response headers, expires"
(let [counter (atom 0)
req {"method" "get" "url" "http://borge.by/jarl" "cache" true}]
(with-redefs [http-client/send-request
(fn [_]
(swap! counter inc)
{:status 200
:headers {"content-type" "application/json"
"date" "Sat, 01 Jan 2022 15:10:05 GMT"
"expires" "Sat, 01 Jan 2022 15:40:05 GMT"}
:body "{\"foo\":\"bar\"}"})]
(let [res {"body" {"foo" "bar"}
"headers" {"content-type" "application/json"
"date" "Sat, 01 Jan 2022 15:10:05 GMT"
"expires" "Sat, 01 Jan 2022 15:40:05 GMT"}
"raw_body" "{\"foo\":\"bar\"}" "status" "200 OK" "status_code" 200}
now (t/instant (t/zoned-date-time "2022-01-01T15:10:05Z"))
exp (time/instant->ns (t/>> now (t/new-duration 60 :minutes)))
bctx {:time-now-ns (time/instant->ns now)}]
(is (= (builtin-http-send {:args [req] :builtin-context bctx}) res))
(is (= @counter 1))
(is (= (builtin-http-send {:args [req] :builtin-context bctx}) res))
(is (= @counter 1))
(is (= (builtin-http-send {:args [req] :builtin-context {:time-now-ns exp}}) res)) ; fast-forward 1 hour
(is (= @counter 2)))))))
borgeby/jarl
(ns jarl.builtins.time-test
(:require [clojure.test :refer [deftest]]
[tick.core :as t]
[jarl.time :as time]
[test.utils :refer [testing-builtin]]))
(deftest builtin-time-clock-test
(let [ns (time/instant->ns (time/parse-iso-datetime "2022-01-01T01:02:03"))]
(testing-builtin "time.clock"
[ns] [1 2 3])))
(deftest builtin-time-date-test
(let [ns (time/instant->ns (time/parse-iso-datetime "2022-03-04T01:02:03"))]
(testing-builtin "time.date"
[ns] [2022 3 4])))
;(deftest builtin-time-diff-test
; (let [ns1 (instant->ns (parse-iso-datetime "2022-01-01T00:00:00"))
; ns2 (instant->ns (parse-iso-datetime "2023-02-02T01:02:03"))]
; (testing-builtin "time.diff"
; [ns1 ns2] [1 1 1 1 1 3])))
(deftest builtin-time-weekday-test
(let [ns (time/instant->ns (time/parse-iso-datetime "2022-01-01T00:00:00"))]
(testing-builtin "time.weekday"
[1655283296943749000] "Wednesday"
[1656284296943749000] "Sunday"
[[ns "UTC"]] "Saturday"
[[ns "Europe/Stockholm"]] "Saturday"
[[ns "America/Anchorage"]] "Friday"
; exceptions
#?@(:clj [[9655283296943749000] [:jarl.exceptions/builtin-exception "timestamp too big"]]))))
(deftest builtin-time-parse-ns-test
(let [ref-time (time/instant->ns (time/parse-iso-zoned-datetime "2022-01-01T12:12:12.00-00:00")) ; 1641039132000000000
ref-time-tz-offset (time/instant->ns (-> (time/ns->instant ref-time) (t/>> (t/of-hours 8))))]
(testing-builtin "time.parse_ns"
["Mon Jan 02 15:04:05 2006" "Sat Jan 01 12:12:12 2022"] ref-time
; ANSIC
["Mon Jan _2 15:04:05 2006" "Sat Jan _1 12:12:12 2022"] ref-time
; UnixDate
["Mon Jan _2 15:04:05 MST 2006" "Sat Jan _1 12:12:12 -0000 2022"] ref-time
["Mon Jan _2 15:04:05 MST 2006" "Sat Jan _1 12:12:12 -0800 2022"] ref-time-tz-offset
; RubyDate
["Mon Jan 02 15:04:05 -0700 2006" "Sat Jan 01 12:12:12 -0800 2022"] ref-time-tz-offset
; Reference time format
["01/02 03:04:05PM '06 -0700" "01/01 12:12:12PM '22 -0000"] ref-time
["01/02 03:04:05PM '06 -0700" "06/02 07:00:00PM '17 -0700"] 1496455200000000000
; Date only
["2006-01-02" "2022-01-01"] 1640995200000000000
; exceptions
#?@(:clj [["2006-01-02T15:04:05Z07:00" "2262-04-11T23:47:16.854775808-00:00"]
[:jarl.exceptions/builtin-exception "time outside of valid range"]]))))