Back
clock (clj)
(source)protocol
(clock _)
Make a clock
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))))))))
;; TODO: Clock tests
;; Create with a value for a fixed clock. Value can be a time or a zone
(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 "Converting using with-clock"
(t/with-clock (t/clock (t/zone "America/New_York"))
(testing "inst to zoned-date-time"
(is (t/= (t/zoned-date-time "2019-08-07T16:00Z")
(t/zoned-date-time "2019-08-07T12:00-04:00[America/New_York]"))))
(testing "date-time to zoned-date-time"
(is (t/= (t/zoned-date-time (t/date-time "2019-08-07T12:00"))
(t/zoned-date-time "2019-08-07T12:00-04:00[America/New_York]"))))
(testing "date-time to offset-date-time"
(is (t/= (t/offset-date-time (t/date-time "2019-08-07T12:00"))
(t/offset-date-time "2019-08-07T12:00-04:00"))))))
(testing "Creating a clock with a zone, and returning that zone"
(is (= "America/New_York" (str (t/zone (t/clock (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")))))))
(testing "Creation of clock with fixed offset"
(is (= "+01:00" (str (t/zone (t/clock (t/offset-date-time "2017-10-31T16:00:00+01:00"))))))))
;; 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))))))
(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 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))))
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])))
(deftest group-by-intervals-test
(testing "p and s"
(t/with-clock t/UTC
(is
(=
{(t/year 2017) [(ti/new-interval
(t/date-time "2017-12-20T00:00")
(t/date-time "2018-01-01T00:00"))]
(t/year 2018) [(ti/new-interval
(t/date-time "2018-01-01T00:00")
(t/date-time "2018-01-10T00:00"))]}
(ti/group-by
(ti/divide (ti/bounds (t/year 2016) (t/year 2019)) t/year)
[(ti/new-interval (t/date-time "2017-12-20T00:00")
(t/date-time "2018-01-10T00:00"))])))))
(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))))))