Back
hours (clj)
(source)function
(hours v)
extract hours from '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 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"))))))
(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 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))))
(deftest divide-test
(is
;; Duration -> Long -> Duration
(= (t/new-duration 6 :hours) (t/divide (t/new-duration 6 :days) 24))
;; Duration -> Duration -> Long
(= 63 (t/divide (t/new-duration 21 :days) (t/new-duration 8 :hours)))))
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 division-test2
(is (= 365 (count (ti/divide-by t/date (t/year 2017)))))
(is (= 12 (count (ti/divide-by t/year-month (t/year 2017)))))
(is (= 30 (count (ti/divide-by t/date (t/year-month "2017-09")))))
(is (= (t/date "2017-09-01") (first (ti/divide-by t/date (t/year-month "2017-09")))))
(is (= (t/date "2017-09-30") (last (ti/divide-by t/date (t/year-month "2017-09")))))
(is (= 31 (count (ti/divide-by t/date (t/year-month "2017-10")))))
(is (= 8 (count (ti/divide-by t/date (ti/bounds (t/date "2017-10-03") (t/date "2017-10-10"))))))
(is (= [(t/date "2017-09-10")] (ti/divide-by t/date (ti/bounds (t/date-time "2017-09-10T12:00")
(t/date-time "2017-09-10T14:00")))))
(is (= [(t/date "2017-09-10") (t/date "2017-09-11")] (ti/divide-by t/date (ti/bounds (t/date-time "2017-09-10T12:00") (t/date-time "2017-09-11T14:00")))))
(is (= 2 (count (ti/divide-by t/year-month (ti/bounds (t/date "2017-09-10") (t/date "2017-10-10"))))))
(is (= 3 (count (ti/divide-by t/year (ti/bounds (t/date-time "2017-09-10T12:00") (t/year "2019"))))))
(is (= 3 (count (ti/divide-by t/year (ti/bounds (t/date-time "2017-09-10T12:00") (t/year-month "2019-02"))))))
(is (= 24 (count (ti/divide-by (t/new-duration 1 :hours) (t/date "2017-09-10"))))))
(deftest concur-test2
(is
(= 2
(t/hours
(t/duration
(ti/concur (ti/new-interval (t/at (t/today) "16:00")
(t/end (t/today)))
(t/today)
(ti/new-interval (t/at (t/today) "20:00")
(t/at (t/today) "22:00"))))))))
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"]]))))