Back

days (clj)

(source)

protocol

(days _)
Return the given quantity in days.

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

  (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 units-test
  (is (=
        {:seconds 0, :nanos 1}
        (t/units (t/new-duration 1 :nanos))))
  (is
    (=
      {:years 10, :months 0, :days 0}
      (t/units (t/new-period 10 :years)))))

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

(deftest day-of-week
  (let [days (fn [strings] (map t/day-of-week strings))]
    (is (every? #{cljc.java-time.day-of-week/sunday} (days ["sun" "sunday"])))
    (is (every? #{cljc.java-time.day-of-week/monday} (days ["mon" "monday"])))
    (is (every? #{cljc.java-time.day-of-week/tuesday} (days ["tue" "tues" "tuesday"])))
    (is (every? #{cljc.java-time.day-of-week/wednesday} (days ["wed" "weds" "wednesday"])))
    (is (every? #{cljc.java-time.day-of-week/thursday} (days ["thur" "thurs" "thursday"])))
    (is (every? #{cljc.java-time.day-of-week/friday} (days ["fri" "friday"])))
    (is (every? #{cljc.java-time.day-of-week/saturday} (days ["sat" "saturday"])))))

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


;; Periods. Convenience functions to create periods of specific
;; units.
(deftest period-functions-test
  (is (= (t/of-days 10) (t/new-period 10 :days)))
  (is (= (t/of-months 10) (t/new-period 10 :months)))
  (is (= (t/of-years 10) (t/new-period 10 :years))))

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