Public Vars

Back

duration (clj)

(source)

function

(duration x)
return Duration or Period (whichever appropriate based on type) contained within the range of ITimeSpan x

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 clock-test
  (testing "clock"
    (t/with-clock (-> (t/date "2018-02-14") (t/at "10:00") (t/in "America/New_York"))
      (testing "(clock) return type"
        (is (t/clock? (t/clock))))
      (testing "Time shifting the clock back by 2 hours"
        (is (= "2018-02-14T13:00:00Z" (str (t/instant (t/<< (t/clock) (t/new-duration 2 :hours)))))))
      (testing "with instant"
        (is (= (t/zone (t/clock (t/instant)))
              (t/zone "America/New_York"))))))


(deftest constructor-test
  (is (t/year? (t/year 2017)))
  (is (= 2017 (cljc.java-time.year/get-value (t/year 2017))))
  (is (t/month? (t/month 12)))
  (is (= t/DECEMBER (t/month 12)))
  (is (= (t/new-date 3030 3 3)
         (t/date "3030-03-03")))
  (is (-> (t/new-duration 1000 :millis)
          (t/inst)
          (t/instant)
          (cljc.java-time.instant/to-epoch-milli)
          (= 1000)))
  (is (= (t/new-year-month 2020 7)
         (t/year-month  "2020-07"))))

(deftest addition-test
  (is (= (t/new-duration 5 :seconds) (t/+ (t/new-duration 2 :seconds) (t/new-duration 3 :seconds))))
  (is (= (t/new-duration 2 :minutes) (t/+ (t/new-duration 90 :seconds) (t/new-duration 30 :seconds)))))

(deftest subtraction-test
  (is (= (t/new-duration 3 :seconds) (t/- (t/new-duration 5 :seconds) (t/new-duration 2 :seconds)))))

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

  (testing "Instants and ZonedDateTimes should be equals if represents the same point in time"
    (is (t/=
          (t/instant (t/clock (t/instant "2017-10-31T16:00:00Z")))
          (t/zoned-date-time "2017-10-31T16:00:00Z[UTC]"))))
  (is
    (t/<
      (t/now)
      (t/>> (t/now) (t/new-duration 10 :seconds))
      (t/>> (t/now) (t/new-duration 20 :seconds))))
  (is
    (t/>
      (t/>> (t/now) (t/new-duration 20 :seconds))
      (t/>> (t/now) (t/new-duration 10 :seconds))
      (t/now)))
  (is (not
        (t/<
          (t/now)
          (t/>> (t/now) (t/new-duration 20 :seconds))
          (t/>> (t/now) (t/new-duration 10 :seconds)))))
  (let [at (t/now)]
    (is (t/<= at at (t/>> at (t/new-duration 1 :seconds))))
    (is (t/>= at at (t/<< at (t/new-duration 10 :seconds)))))

  (testing "durations"
    (is (t/> (t/new-duration 20 :seconds) (t/new-duration 10 :seconds)))
    (is (t/>= (t/new-duration 20 :seconds) (t/new-duration 20 :seconds)))
    (is (t/>= (t/new-duration 20 :seconds) (t/new-duration 19 :seconds)))
    (is (not (t/>= (t/new-duration 19 :seconds) (t/new-duration 20 :seconds))))
    (is (t/< (t/new-duration 10 :seconds) (t/new-duration 20 :seconds)))
    (is (t/<= (t/new-duration 20 :seconds) (t/new-duration 20 :seconds)))
    (is (t/<= (t/new-duration 19 :seconds) (t/new-duration 20 :seconds)))
    (is (not (t/<= (t/new-duration 20 :seconds) (t/new-duration 19 :seconds))))))

;; Durations. Simple constructors to create durations of specific
;; units.

(deftest duration-test
  (is (= (t/new-duration 1e6 :nanos) (t/new-duration 1 :millis)))
  (is (= (t/new-duration 1e9 :nanos) (t/new-duration 1 :seconds)))
  (is (= (t/new-duration 1000 :millis) (t/new-duration 1 :seconds)))

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

;; Durations. Convenience functions to create durations of specific
;; units.
(deftest duration-functions-test
  (is (= (t/of-nanos 10) (t/new-duration 10 :nanos)))
  (is (= (t/of-micros 10) (t/new-duration 10 :micros))) ;java.time.Duration doesn't have ofMicros method
  (is (= (t/of-millis 10) (t/new-duration 10 :millis)))
  (is (= (t/of-seconds 10) (t/new-duration 10 :seconds)))
  (is (= (t/of-minutes 10) (t/new-duration 10 :minutes)))
  (is (= (t/of-hours 10) (t/new-duration 10 :hours))))


(deftest predicates-test
  (is (true? (t/clock? (t/clock))))
  (is (true? (t/day-of-week? t/MONDAY)))
  (is (true? (t/duration? (t/new-duration 1 :minutes))))
  (is (true? (t/instant? (t/instant))))
  (is (true? (t/date? (t/today))))
  (is (true? (t/date-time? (t/at (t/today) (t/new-time 0 0)))))
  (is (true? (t/time? (t/new-time 0 0))))
  (is (true? (t/month? t/MAY)))
  (is (true? (t/offset-date-time? (t/offset-date-time))))
  (is (true? (t/period? (t/new-period 1 :weeks))))
  (is (true? (t/year? (t/year))))
  (is (true? (t/year-month? (t/year-month))))
  (is (true? (t/zone? (t/zone))))
  (is (true? (t/zone-offset? (t/zone-offset (t/zoned-date-time)))))
  (is (true? (t/zoned-date-time? (t/zoned-date-time))))
  (is (false? (t/date? 16)))
  (is (false? (t/month? 16))))

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

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

;; TODO: Divide by duration

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

(defn moment [t]
  (ti/new-interval
    t
    (t/>> t (t/new-duration 3 :seconds))))
lumberdev/tesserae
(ns tesserae.serialize
  (:require [tick.core :as t]
            [time-literals.read-write]
            [cognitect.transit :as transit]
            [hyperfiddle.electric :as e]
            #?(:cljs
               [java.time :refer [Period
                                  LocalDate
                                  LocalDateTime
                                  ZonedDateTime
                                  OffsetTime
                                  Instant
                                  OffsetDateTime
                                  ZoneId
                                  DayOfWeek
                                  LocalTime
                                  Month
                                  Duration
                                  Year
                                  YearMonth]]))
  #?(:clj (:import
            (java.time Period
                       LocalDate
                       LocalDateTime
                       ZonedDateTime
                       OffsetTime
                       Instant
                       OffsetDateTime
                       ZoneId
                       DayOfWeek
                       LocalTime
                       Month
                       Duration
                       Year
                       YearMonth))))


(def time-classes
  {'time/period          Period
   'time/date            LocalDate
   'time/date-time       LocalDateTime
   'time/zoned-date-time ZonedDateTime
   'time/instant         Instant
   ;;'offset-time OffsetTime
   ;;'offset-date-time OffsetDateTime
   'time/time            LocalTime
   'time/duration        Duration
   'time/year            Year
   'time/year-month      YearMonth
   'time/zone            ZoneId
   'time/day-of-week     DayOfWeek
   'time/month           Month})
borgeby/jarl
(ns jarl.builtins.http-test
  (:require [jarl.builtins.http :refer [builtin-http-send content-type create-request parse-timeout]]
            [jarl.exceptions :as errors]
            [jarl.types :refer [rego-equal?]]
            [jarl.http-client :as http-client]
            [jarl.time :as time]
            #?(:clj  [clojure.test :refer [deftest is testing]]
               :cljs [cljs.test :refer [deftest is testing]])
            [tick.core :as t]))

(deftest create-request-test
  (testing "create request, default values"
    (is (= (create-request                {"method" "get" "url" "https://foo.bar"})
           {:follow-redirects             false
            :force-cache                  false
            :force-cache-duration-seconds 0
            :force-json-decode            false
            :force-yaml-decode            false
            :insecure?                    false
            :raise-error                  true
            :conn-timeout                 5000
            :method                       :get
            :url                          "https://foo.bar"})))

  (testing "create request, modified values"
    (is (= (create-request
             {"method"                        "PUT"
              "url"                           "https://foo.bar"
              "force_cache"                   true
              "force_cache_duration_seconds"  15
              "enable_redirect"               true
              "force_yaml_decode"             true
              "tls_insecure_skip_verify"      true
              "timeout"                       "1s500ms"
              "raise_error"                   false})
           {:follow-redirects             true
            :force-cache                  true
            :force-cache-duration-seconds 15
            :force-json-decode            false
            :force-yaml-decode            true
            :insecure?                    true
            :raise-error                  false
            :conn-timeout                 1500
            :method                       :put
            :url                          "https://foo.bar"}))))

  (testing "force cache"
    (let [counter (atom 0)
          req {"method" "get" "url" "http://borge.by" "force_cache" true "force_cache_duration_seconds" 30}]
      (with-redefs [http-client/send-request
                    (fn [_]
                      (swap! counter inc)
                      {:status 200 :headers {"content-type" "application/json"} :body "{\"foo\":\"bar\"}"})]
        (let [res {"body" {"foo" "bar"} "headers" {"content-type" "application/json"}
                   "raw_body" "{\"foo\":\"bar\"}" "status" "200 OK" "status_code" 200}]
          (is (= (builtin-http-send {:args [req] :builtin-context {:time-now-ns 1}}) res))
          (is (= (builtin-http-send {:args [req] :builtin-context {:time-now-ns 1}}) res))
          (is (= (builtin-http-send {:args [req] :builtin-context {:time-now-ns 1}}) res))
          (is (= @counter 1)))))))

(deftest http-send-caching-test
  (testing "force cache"
    (let [counter (atom 0)
          req {"method" "get" "url" "http://borge.by/foo" "force_cache" true "force_cache_duration_seconds" 30}]
      (with-redefs [http-client/send-request
                    (fn [_]
                      (swap! counter inc)
                      {:status 200 :headers {"content-type" "application/json"} :body "{\"foo\":\"bar\"}"})]
        (let [res {"body" {"foo" "bar"} "headers" {"content-type" "application/json"}
                   "raw_body" "{\"foo\":\"bar\"}" "status" "200 OK" "status_code" 200}]
          (is (= (builtin-http-send {:args [req] :builtin-context {:time-now-ns 1}}) res))
          (is (= (builtin-http-send {:args [req] :builtin-context {:time-now-ns 1}}) res))
          (is (= @counter 1))))))

  (testing "force cache duration"
    (let [counter (atom 0)
          req {"method" "get" "url" "http://borge.by/bar" "force_cache" true "force_cache_duration_seconds" 30}]
      (with-redefs [http-client/send-request
                    (fn [_]
                      (swap! counter inc)
                      {:status 200 :headers {"content-type" "application/json"} :body "{\"foo\":\"bar\"}"})]
        (let [res {"body" {"foo" "bar"} "headers" {"content-type" "application/json"}
                   "raw_body" "{\"foo\":\"bar\"}" "status" "200 OK" "status_code" 200}
              now   (time/now-ns)
              exp   (time/instant->ns (t/>> (t/now) (t/new-duration 60 :seconds)))
              bctx {:time-now-ns now}]
          (is (= (builtin-http-send {:args [req] :builtin-context bctx}) res))
          (is (= @counter 1))
          (is (= (builtin-http-send {:args [req] :builtin-context bctx}) res))
          (is (= @counter 1))
          (is (= (builtin-http-send {:args [req] :builtin-context {:time-now-ns exp}}) res)) ; fast-forward 1 minute
          (is (= @counter 2))))))

  (testing "cache from response headers, cache-control"
    (let [counter (atom 0)
          req {"method" "get" "url" "http://borge.by" "cache" true}]
      (with-redefs [http-client/send-request
                    (fn [_]
                      (swap! counter inc)
                      {:status 200
                       ; note upper-case keys
                       :headers {"Content-Type" "application/json"
                                 "Cache-Control" "ignored, max-age=30, also=ignored"
                                 "Date" "Sat, 01 Jan 2022 15:10:05 GMT"}
                       :body "{\"foo\":\"bar\"}"})]
        (let [res {"body" {"foo" "bar"}
                   "headers" {"content-type" "application/json"
                              "cache-control" "ignored, max-age=30, also=ignored"
                              "date" "Sat, 01 Jan 2022 15:10:05 GMT"}
                   "raw_body" "{\"foo\":\"bar\"}" "status" "200 OK" "status_code" 200}
              now   (t/instant (t/zoned-date-time "2022-01-01T15:10:05Z"))
              exp   (time/instant->ns (t/>> now (t/new-duration 60 :seconds)))
              bctx {:time-now-ns (time/instant->ns now)}]
          (is (= (builtin-http-send {:args [req] :builtin-context bctx}) res))
          (is (= @counter 1))
          (is (= (builtin-http-send {:args [req] :builtin-context bctx}) res))
          (is (= @counter 1))
          (is (= (builtin-http-send {:args [req] :builtin-context {:time-now-ns exp}}) res)) ; fast-forward 1 minute
          (is (= @counter 2))))))

  (testing "cache from response headers, expires"
    (let [counter (atom 0)
          req {"method" "get" "url" "http://borge.by/jarl" "cache" true}]
      (with-redefs [http-client/send-request
                    (fn [_]
                      (swap! counter inc)
                      {:status 200
                       :headers {"content-type" "application/json"
                                 "date" "Sat, 01 Jan 2022 15:10:05 GMT"
                                 "expires" "Sat, 01 Jan 2022 15:40:05 GMT"}
                       :body "{\"foo\":\"bar\"}"})]
        (let [res {"body" {"foo" "bar"}
                   "headers" {"content-type" "application/json"
                              "date" "Sat, 01 Jan 2022 15:10:05 GMT"
                              "expires" "Sat, 01 Jan 2022 15:40:05 GMT"}
                   "raw_body" "{\"foo\":\"bar\"}" "status" "200 OK" "status_code" 200}
              now   (t/instant (t/zoned-date-time "2022-01-01T15:10:05Z"))
              exp   (time/instant->ns (t/>> now (t/new-duration 60 :minutes)))
              bctx {:time-now-ns (time/instant->ns now)}]
          (is (= (builtin-http-send {:args [req] :builtin-context bctx}) res))
          (is (= @counter 1))
          (is (= (builtin-http-send {:args [req] :builtin-context bctx}) res))
          (is (= @counter 1))
          (is (= (builtin-http-send {:args [req] :builtin-context {:time-now-ns exp}}) res)) ; fast-forward 1 hour
          (is (= @counter 2)))))))
borgeby/jarl
(ns jarl.builtins.time-test
  (:require [clojure.test :refer [deftest]]
            [tick.core :as t]
            [jarl.time :as time]
            [test.utils :refer [testing-builtin]]))

(deftest builtin-time-parse-duration-ns-test
  (testing-builtin "time.parse_duration_ns"
    ["1ns"]                     1
    ["1ns2ms"]                  2000001
    ["1h"]                      3600000000000
    ["33h11m59s7ms99us12345ns"] 119519007111345
    ; exceptions
    ["h"]                       [:jarl.exceptions/builtin-exception "time: invalid duration \"h\""]
    ["33"]                      [:jarl.exceptions/builtin-exception "time: missing unit in duration \"33\""]
    ["33ks"]                    [:jarl.exceptions/builtin-exception "time: unknown unit \"ks\" in duration \"33ks\""]))