Back

end (clj)

(source)

protocol

(end _)
Return the end of a span of time

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])))

(extend-protocol p/ITimeSpan
  ; as required by some tests in this ns
  Instant
  (beginning [i] i)
  (end [i] i))

  (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 range-test
  (is (t/midnight? (t/beginning (t/today))))
  (is (t/midnight? (t/end (t/today))))
  (is (t/midnight? (t/beginning (t/year))))
  (is (t/midnight? (t/end (t/year)))))

(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))}))))
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])))

(extend-protocol p/ITimeSpan
  ; as required by some tests in this ns
  Instant
  (beginning [i] i)
  (end [i] i)
  LocalDateTime
  (beginning [i] i)
  (end [i] i))

(deftest normalize-test
  (let [intervals
        [(ti/new-interval (t/date "2017-06-15") (t/date "2017-06-25"))
         (ti/new-interval (t/date "2017-06-26") (t/date "2017-06-28"))
         (ti/new-interval (t/date "2017-06-30") (t/date "2017-07-04"))]]
    (is (= [{:tick/intervals
             [{:tick/beginning (t/date-time "2017-06-15T00:00")
               :tick/end (t/date-time "2017-06-26T00:00")}
              {:tick/beginning (t/date-time "2017-06-26T00:00")
               :tick/end (t/date-time "2017-06-29T00:00")}]}
            {:tick/intervals
             [{:tick/beginning (t/date-time "2017-06-30T00:00")
               :tick/end (t/date-time "2017-07-05T00:00")}]}]

;; We are reclaiming 'disjoin' to mean to 'end the joining of' or 'to become disjoint'.

;; TODO: more Interval testing
(deftest successive-intervals-meet
  (doseq [x [(t/date) (t/year) (t/year-month)]]
    (is (ti/meets? (t/end x) (t/beginning (t/inc x))))))

(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"))))))))

;; Can we disturb?
(deftest cannot-disturb-test
  (extend-protocol p/ITimeSpan
    LocalTime
    (beginning [i] i)
    (end [i] i))
  (let
    [disturb-interval [(ti/new-interval (t/time "07:00") (t/time "22:00"))]
     no-disturb-interval (ti/complement disturb-interval)
     can-disturb? (fn [t] (not (some #(t/coincident? % t) no-disturb-interval)))
     ]
    (is (not (can-disturb? (t/time "03:00"))))
    (is (not (can-disturb? (t/time "07:00"))))
    (is (can-disturb? (t/time "07:01")))
    (is (can-disturb? (t/time "12:00")))
    (is (can-disturb? (t/time "21:59")))
    (is (not (can-disturb? (t/time "22:00"))))
    (is (not (can-disturb? (t/time "00:00"))))))