Skip to content

Commit

Permalink
change beginning/end functions to only work for ITimeSpan instances. …
Browse files Browse the repository at this point in the history
…ITimeSpan no longer implemented for types where beginning/end were the same - to recreate earlier behaviour, run `(t/backward-compatible-time-span-extensions)`
  • Loading branch information
henryw374 committed Sep 5, 2023
1 parent 5b5545c commit 3b84eb5
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* tick.interval.bounds function is now consistent for years, year-months and dates in setting the `end` to be inclusive
* Strings are no longer accepted as beginning/end when constructing an interval
* Strings are no longer accepted as arguments to clock
* ITimeSpan no longer implemented for types where beginning/end were the same - to recreate earlier behaviour, run `(t/backward-compatible-time-span-extensions)`

# 0.6.1

Expand Down
12 changes: 5 additions & 7 deletions docs/cheatsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@

# Naming (compared to java.time)

LocalDate => `date`

LocalDateTime => `date-time`

java.util.Date => `inst`

js/Date => `inst`
* LocalDate => `date`
* LocalDateTime => `date-time`
* LocalTime => `time`
* java.util.Date => `inst`
* js/Date => `inst`

otherwise all camel-case equivalents of java.time names

Expand Down
39 changes: 37 additions & 2 deletions src/tick/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -959,8 +959,8 @@

;;;
(defn between "the span of time between v1 and v2" [v1 v2] (p/between v1 v2))
(defn beginning "the beginning of the range of ITimeSpan v or v" [v] (if (satisfies? p/ITimeSpan v) (p/beginning v) v))
(defn end "the end of the range of ITimeSpan v or v" [v] (if (satisfies? p/ITimeSpan v) (p/end v) v))
(defn beginning "the beginning of the range of ITimeSpan v or v" [v] (p/beginning v))
(defn end "the end of the range of ITimeSpan v or v" [v] (p/end v))

(defn duration "return duration contained within the range of ITimeSpan 'x', which can be a year, year-month or date " [x]
(cljc.java-time.duration/between (beginning x) (end x)))
Expand Down Expand Up @@ -988,6 +988,7 @@
;; TODO: Test concurrent? in tick.core-test

(extend-protocol p/ITimeSpan
; ITimeSpan is implemented by default on types with a natural beginning and end
LocalDate
(beginning [date] (cljc.java-time.local-date/at-start-of-day date))
(end [date] (cljc.java-time.local-date/at-start-of-day (inc date)))
Expand All @@ -1000,6 +1001,40 @@
(beginning [ym] (beginning (cljc.java-time.year-month/at-day ym 1)))
(end [ym] (beginning (cljc.java-time.year-month/at-day (inc ym) 1))))

(defn backward-compatible-time-span-extensions
"pre v0.7, ITimeSpan was extended as per this body. run this function to create those extensions.
ITimeSpan is implemented by default on types with a natural beginning and end"
[]
(extend-protocol p/ITimeSpan
Instant
(beginning [i] i)
(end [i] i)

ZonedDateTime
(beginning [i] i)
(end [i] i)

OffsetDateTime
(beginning [i] i)
(end [i] i)

#?(:clj Date :cljs js/Date)
(beginning [i] (p/instant i))
(end [i] (p/instant i))

LocalDateTime
(beginning [x] x)
(end [x] x)

LocalTime
(beginning [x] x)
(end [x] x)

nil
(beginning [_] nil)
(end [_] nil)))

(extend-protocol p/ITimeReify
LocalTime
(on [t d] (cljc.java-time.local-time/at-date t (p/date d)))
Expand Down
18 changes: 17 additions & 1 deletion test/tick/alpha/interval_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@
[clojure.test
:refer [deftest is testing run-tests]
:refer-macros [deftest is testing run-tests]]
[tick.alpha.interval :as ti]))
[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))

(s/check-asserts true)

Expand Down Expand Up @@ -714,6 +726,10 @@

;; 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)
Expand Down
12 changes: 11 additions & 1 deletion test/tick/api_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@
: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]))
[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))

(deftest time-construction-test
(testing "(time)"
Expand Down

0 comments on commit 3b84eb5

Please sign in to comment.