Back

truncate (clj)

(source)

protocol

(truncate date-time unit-kw)

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 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 comparison-test
  (let [point (t/truncate (t/instant) :millis)
        later (t/>> point (t/new-duration 1 :millis))]
    (testing "shifting inst"
      (let [i (t/inst)]
        (is (= i (-> i
                     (t/>> (t/new-duration 10 :seconds))
                     (t/<< (t/new-duration 10 :seconds)))))))
    (testing "max-min"
      (is (= later (t/max point later point later)))
      (is (= point (t/min point later point later))))
    (testing "max-min key"
      (is (= {:foo later} (t/max-key :foo {:foo point} {:foo later} {:foo point} {:foo later})))
      (is (= {:foo point} (t/min-key :foo {:foo point} {:foo later} {:foo point} {:foo later}))))
    (testing "comparables not="
      (doseq [point (point-in-time-comparable point)]
        (testing "comparables ="
          (is (apply t/= point (point-in-time-comparable point)))
          (is (apply t/>= point (point-in-time-comparable point))))
        (is (apply t/<= point (point-in-time-comparable later))))
      (doseq [later (point-in-time-comparable later)]
        (is (apply t/>= later (point-in-time-comparable point))))