Back
between (clj)
(source)function
(between v1 v2)
(between v1 v2 unit)
for the 2-arity version, find the temporal-amount between v1 and v2,
or for the 3-arity version the amount of 'unit' between v1 and v2
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])))
;; Between test
(deftest between-test
(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))))))
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])))
;; Example: We mustn't disturb people between 10pm and 7am the following morning, in their locale.
;; TODO: Think about conversions between single instants and intervals. Feather? Widen? Smudge?