Skip to content
Browse files

earliest and latest functions added for choosing from DateTimes

  • Loading branch information...
1 parent b9d78e4 commit f65ddc09108b702351909d1829f237296a17eead @coltnz committed
Showing with 33 additions and 0 deletions.
  1. +11 −0 src/clj_time/core.clj
  2. +22 −0 test/clj_time/core_test.clj
View
11 src/clj_time/core.clj
@@ -419,6 +419,17 @@
[^Period period]
(plus (now) period))
+(defn earliest
+ "Returns the earliest of the supplied DateTime(s)"
+ [& dts]
+ (reduce (fn [dt1 dt2]
+ (if (pos? (compare dt1 dt2)) dt2 dt1)) dts))
+
+(defn latest [& dts]
+ "Returns the latest of the supplied DateTime(s)"
+ (reduce (fn [dt1 dt2]
+ (if (neg? (compare dt1 dt2)) dt2 dt1)) dts))
+
(defn interval
"Returns an interval representing the span between the two given ReadableDateTimes.
Note that intervals are closed on the left and open on the right."
View
22 test/clj_time/core_test.clj
@@ -239,6 +239,28 @@
(is (= (-> 30 minutes from-now)
(date-time 2011 4 16 10 39 00))))))
+(deftest test-earliest
+ (let [d1 (date-time 1990 1 1 23 1 1)
+ d2 (date-time 2000 1 1 23 1 1)
+ d3 (date-time 2010 1 1 23 1 1)
+ d4 (date-time 2020 1 1 23 1 1)]
+ (is (= d1 (earliest d1 d2 d3 d4)))
+ (is (= d1 (earliest d4 d2 d3 d1)))
+ (is (= d2 (earliest d4 d3 d2)))
+ (is (= d4 (earliest d4)))
+ (is (= Exception) (earliest d1 d2 nil))))
+
+(deftest test-latest
+ (let [d1 (date-time 1990 1 1 23 1 1)
+ d2 (date-time 2000 1 1 23 1 1)
+ d3 (date-time 2010 1 1 23 1 1)
+ d4 (date-time 2020 1 1 23 1 1)]
+ (is (= d4 (latest d1 d2 d3 d4)))
+ (is (= d4 (latest d4 d2 d3 d1)))
+ (is (= d3 (latest d2 d3 d1)))
+ (is (= d1 (latest d1)))
+ (is (= Exception) (latest d1 d2 nil))))
+
(deftest test-start-end
(let [s (date-time 1986 10 14 12 5 4)
e (date-time 1986 11 3 22 2 6)

0 comments on commit f65ddc0

Please sign in to comment.
Something went wrong with that request. Please try again.