Back
zone (clj)
(source)function
(zone)
(zone z)
Examples
tick
(ns tick.addon-libs-test
(:require
[tick.core :as t]
[tick.timezone]
[tick.locale-en-us]
[clojure.test
:refer [deftest is testing run-tests]
:refer-macros [deftest is testing run-tests]]))
(deftest tz-test
(is (t/zone "Europe/Berlin")))
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])))
;; 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 offset"
(is (= "+01:00" (str (t/zone (t/clock (t/offset-date-time "2017-10-31T16:00:00+01:00"))))))))
(deftest extraction-test
(is (= 2 (t/int t/FEBRUARY)))
(is (= 2 (t/int t/TUESDAY)))
(is (= t/AUGUST (t/month (t/date-time "2017-08-08T12:00:00"))))
(is (= t/AUGUST (t/month (t/year-month "2017-08"))))
(is (= (t/year 2019) (t/year (t/zoned-date-time "2019-09-05T00:00:00+02:00[Europe/Oslo]"))))
(is (= (t/year 2019) (t/year (t/offset-date-time "2019-09-05T00:00:00-03:00"))))
(is (= (t/zone-offset "-04:00")
(t/zone-offset (t/zoned-date-time "2019-03-15T15:00-04:00[America/New_York]"))))
(is (= (t/zone-offset "-04:00")
(t/zone-offset (t/offset-date-time "2019-03-15T15:00-04:00")))))
(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 offset-date-time-test
(let [t "2018-09-24T18:57:08.996+01:00"]
(testing "offset date time basics"
(is (t/offset-date-time? (t/offset-date-time (t/now))))
(is (t/offset-date-time? (t/offset-date-time t)))
(is (t/offset-date-time? (t/offset-date-time (t/date-time))))
(is (t/offset-date-time? (t/offset-date-time (t/zoned-date-time)))))))
(deftest zoned-date-time-test
(is (t/zoned-date-time? (t/zoned-date-time "2020-12-15T12:00:10Z[Europe/London]")))
(is (t/zoned-date-time? (t/zoned-date-time "2020-12-15T12:00:10+04:00[Europe/London]"))))
(deftest fields-test
(let [xs [(t/now)
(t/zoned-date-time)
(t/offset-date-time)
(t/date-time)
(t/date)
(t/time)
(t/year)
(t/year-month)]]
(doseq [x xs]
(let [fields (t/fields x)
fields-map (into {} fields)]
(is (not-empty fields-map))
(doseq [[f v] fields-map]
(is (= v (get fields f)))
(is (= :foo (get fields :bar :foo))))))))
;; Comparison test
(defn point-in-time-comparable [i]
[i
(t/inst i)
(t/zoned-date-time i)
(t/offset-date-time i)])
(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 parse-test
(is (t/date? (t/parse-date "2020/02/02" (t/formatter "yyyy/MM/dd"))))
(is (t/year? (t/parse-year "20" (t/formatter "yy"))))
(is (t/year-month? (t/parse-year-month "20/02" (t/formatter "yy/MM"))))
(is (t/date-time? (t/parse-date-time "2020/02/02:2002" (t/formatter "yyyy/MM/dd:HHmm"))))
(is (t/time? (t/parse-time "2002" (t/formatter "HHmm"))))
(is (t/zoned-date-time? (t/parse-zoned-date-time "2020/02/02:2002:Z"
(t/formatter "yyyy/MM/dd:HHmm:VV"))))
(is (t/offset-date-time? (t/parse-offset-date-time "2020/02/02:2002:-08:30"
(t/formatter "yyyy/MM/dd:HHmm:VV")))))
(testing "ZonedDateTimes in different zones should be equals"
(is (t/=
(t/zoned-date-time "2017-10-31T16:00:00-04:00[America/New_York]")
(t/zoned-date-time "2017-10-31T13:00:00-07:00[America/Los_Angeles]"))))
(testing "ZoneDateTimes and OffsetDateTime should be equals if represents the same point in time"
(is (t/=
(t/zoned-date-time "2017-10-31T16:00:00-04:00[America/New_York]")
(t/offset-date-time "2017-10-31T13:00-07:00"))))
(testing "ZoneDateTimes and platform Date should be equals if represents the same point in time"
(is (t/=
(t/zoned-date-time "2017-10-31T16:00:00-04:00[America/New_York]")
(t/inst "2017-10-31T20:00:00Z"))))
(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))))
(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])))
(deftest date-relation-test
(is (=
(ti/relation
(ti/new-interval
(t/zoned-date-time "2021-02-24T00:00Z[GMT]")
(t/zoned-date-time "2021-02-25T00:00Z[GMT]"))
(ti/new-interval
(t/zoned-date-time "2021-02-23T00:00Z[Europe/London]")
(t/zoned-date-time "2021-02-24T00:00Z[Europe/London]")))
:met-by)))
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 "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-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"]]))))