Back
bounds (clj)
(source)function
(bounds & args)
Examples
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])))
(map ti/bounds
(ti/unite [(ti/new-interval (t/date "2017-06-10") (t/date "2017-06-20"))
(ti/new-interval (t/date "2017-06-15") (t/date "2017-06-25"))
(ti/new-interval (t/date "2017-07-01") (t/date "2017-07-10"))])))))
(testing "Unite containing intervals"
(is
(=
[(ti/new-interval (t/date "2017-06-15") (t/date "2017-06-25"))]
(map ti/bounds
(ti/unite [(ti/new-interval (t/date "2017-06-15") (t/date "2017-06-25"))
(ti/new-interval (t/date "2017-06-17") (t/date "2017-06-20"))])))))
(testing "Unite finished-by intervals"
(is
(=
[(ti/new-interval (t/date "2017-06-15") (t/date "2017-06-25"))]
(map ti/bounds
(ti/unite [(ti/new-interval (t/date "2017-06-15") (t/date "2017-06-25"))
(ti/new-interval (t/date "2017-06-17") (t/date "2017-06-20"))
(ti/new-interval (t/date "2017-06-18") (t/date "2017-06-25"))]))))))
(let [coll1 [(ti/new-interval (t/date-time "2017-04-11T00:00")
(t/date-time "2017-04-14T00:00"))
(ti/new-interval (t/date-time "2017-04-18T00:00")
(t/date-time "2017-04-20T00:00"))
(ti/new-interval (t/date-time "2017-12-20T00:00")
(t/date-time "2017-12-23T00:00"))
(ti/new-interval (t/date-time "2017-12-27T00:00")
(t/date-time "2018-01-01T00:00"))
(ti/new-interval (t/date-time "2018-01-02T00:00")
(t/date-time "2018-01-08T00:00"))]
coll2 [(ti/bounds (t/year "2017"))]]
(is (= [(ti/new-interval (t/date-time "2017-04-11T00:00")
(t/date-time "2017-04-14T00:00"))
(ti/new-interval (t/date-time "2017-04-18T00:00")
(t/date-time "2017-04-20T00:00"))
(ti/new-interval (t/date-time "2017-12-20T00:00")
(t/date-time "2017-12-23T00:00"))
(ti/new-interval (t/date-time "2017-12-27T00:00")
(t/date-time "2018-01-01T00:00"))]
(ti/intersection coll1 coll2))))
(let [coll1 [(ti/new-interval (t/date-time "2017-04-11T00:00")
(t/date-time "2017-04-14T00:00"))
(ti/new-interval (t/date-time "2017-04-18T00:00")
(t/date-time "2017-04-20T00:00"))
(ti/new-interval (t/date-time "2017-12-20T00:00")
(t/date-time "2017-12-23T00:00"))
(ti/new-interval (t/date-time "2017-12-27T00:00")
(t/date-time "2018-01-01T00:00"))
(ti/new-interval (t/date-time "2018-01-02T00:00")
(t/date-time "2018-01-08T00:00"))]
coll2 [(ti/bounds (t/year "2017"))]]
(is (= [(ti/new-interval (t/date-time "2017-04-11T00:00")
(t/date-time "2017-04-14T00:00"))
(ti/new-interval (t/date-time "2017-04-18T00:00")
(t/date-time "2017-04-20T00:00"))
(ti/new-interval (t/date-time "2017-12-20T00:00")
(t/date-time "2017-12-23T00:00"))
(ti/new-interval (t/date-time "2017-12-27T00:00")
(t/date-time "2018-01-01T00:00"))]
(ti/intersection coll1 coll2)))
(is (= [(ti/bounds (t/date "2017-07-31") (t/date "2017-08-13"))]
(ti/difference
[(ti/bounds (t/date "2017-07-31") (t/date "2017-08-13"))]
[(ti/bounds (t/date "2017-01-01"))])))
#_(deftest disj-test
(is (=
[(ti/new-interval (t/date-time "2017-01-01T00:00")
(t/date-time "2017-07-04T00:00"))
(ti/new-interval (t/date-time "2017-07-05T00:00")
(t/date-time "2018-01-01T00:00"))]
(disj [(bounds "2017")] (bounds (t/date "2017-07-04"))))))
(deftest division-test
(is (= 7 (count (ti/divide (ti/bounds (t/year 2012) (t/year 2018)) t/year)))))
(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"))])))))
(testing "O"
(is
(=
{(t/year 2015) [(ti/bounds (t/year-month "2015-06") (t/year-month "2015-12"))]
(t/year 2016) [(ti/bounds (t/year 2016))]
(t/year 2017) [(ti/bounds (t/year-month "2017-01") (t/year-month "2017-06"))]}
(ti/group-by
(ti/divide (ti/bounds (t/year 2014) (t/year 2018)) t/year)
[(ti/bounds (t/year-month "2015-06") (t/year-month "2017-06"))]))))
(testing "M and e"
(is
(=
{(t/year 2015) [(t/year 2015)]
(t/year 2016) [(t/year 2016)]}
(ti/group-by
(ti/divide (ti/bounds (t/year 2014) (t/year 2017)) t/year)
(ti/divide (ti/bounds (t/year 2015) (t/year 2016)) t/year)))))
(testing "s"
(is (=
{(t/year 2015) [(ti/bounds (t/year 2015))]
(t/year 2016) [(ti/bounds (t/year 2016))]}
(ti/group-by
(ti/divide (ti/bounds (t/year 2014) (t/year 2017)) t/year)
[(ti/bounds (t/year 2015) (t/year 2016))]))))
(testing "f"
(is
(=
{(t/year 2015) [(ti/bounds (t/year-month "2015-06") (t/year-month "2015-12"))]}
(ti/group-by
[(t/year 2014) (t/year 2015) (t/year 2016)]
[(ti/bounds (t/year-month "2015-06") (t/year-month "2015-12"))]))))
(testing "F"
(is
(=
{(ti/bounds (t/year-month "2015-06") (t/year-month "2015-12"))
[(ti/bounds (t/year-month "2015-06") (t/year-month "2015-12"))]}
(ti/group-by
[(ti/bounds (t/year-month "2015-06") (t/year-month "2015-12"))]
[(t/year 2014) (t/year 2015) (t/year 2016)]))))
(testing "d"
(is
(=
{(t/year 2015) [(ti/bounds (t/year-month "2015-03") (t/year-month "2015-09"))]}
(ti/group-by
[(t/year 2014) (t/year 2015) (t/year 2016)]
[(ti/bounds (t/year-month "2015-03") (t/year-month "2015-09"))]))))
(testing "D"
(is
(=
{(ti/bounds (t/year-month "2015-03") (t/year-month "2015-09"))
[(ti/bounds (t/year-month "2015-03") (t/year-month "2015-09"))]}
(ti/group-by
[(ti/bounds (t/year-month "2015-03") (t/year-month "2015-09"))]
[(t/year 2014) (t/year 2015) (t/year 2016)]))))
(testing "o"
(is
(=
{(ti/bounds (t/year-month "2015-06") (t/year-month "2017-06"))
[(ti/bounds (t/year-month "2015-06") (t/year-month "2015-12"))
(t/year "2016")
(ti/bounds (t/year-month "2017-01") (t/year-month "2017-06"))]}
(ti/group-by
[(ti/bounds (t/year-month "2015-06") (t/year-month "2017-06"))]
(ti/divide (ti/bounds (t/year 2014) (t/year 2018)) t/year))))))
(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"))))))