Back
of-hours (clj)
(source)function
(of-hours n)
Takes a java.lang.Long n and returns a duration of n hours.
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 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))))))
(is (= (t/of-hours 24) (t/duration (t/date))))
(is (= (t/of-days 1) (t/duration {:tick/beginning (t/date)
:tick/end (t/inc (t/date))}))))
;; Durations. Convenience functions to create durations of specific
;; units.
(deftest duration-functions-test
(is (= (t/of-nanos 10) (t/new-duration 10 :nanos)))
(is (= (t/of-micros 10) (t/new-duration 10 :micros))) ;java.time.Duration doesn't have ofMicros method
(is (= (t/of-millis 10) (t/new-duration 10 :millis)))
(is (= (t/of-seconds 10) (t/new-duration 10 :seconds)))
(is (= (t/of-minutes 10) (t/new-duration 10 :minutes)))
(is (= (t/of-hours 10) (t/new-duration 10 :hours))))
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"]]))))