Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

earliest and latest functions added for choosing from DateTimes #108

Merged
merged 2 commits into from

3 participants

@coltnz

As discussed on mailing list.

src/clj_time/core.clj
@@ -419,6 +419,17 @@
[^Period period]
(plus (now) period))
+(defn earliest
+ "Returns the earliest of the supplied DateTime(s)"
@michaelklishin Owner

Please make the docstring come after the list of arguments.

@coltnz
coltnz added a note

crap sorry its ridiculous how often I do that

@seancorfield Owner

The docstring should be before the arg list, not after it:

(defn before "docstring" [& args] "ignored" "result")
(defn after [& args] "ignored" "result")
(meta #'before)
{:arglists ([& args]), :ns #<Namespace user>, :name before, :end-column 13, :column 1, :doc "docstring", :line 5, :file "NO_SOURCE_FILE", :end-line 5}
(meta #'after)
{:arglists ([& args]), :ns #<Namespace user>, :name after, :end-column 12, :column 1, :line 7, :file "NO_SOURCE_FILE", :end-line 7}
@michaelklishin Owner

Doh, right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@michaelklishin

Looks good except for one cosmetic thing. However, I'd suggest making this function take a collection/sequence instead of being variadic.

@michaelklishin

Alternatively, it can be based on another couple of function, earliest-of/latest-of, which take a collection and earliest/latest will be just built on top of that. Invoking variadic functions with a list
of arguments constructed dynamically is a pain, having a non-variadic alternative would help.

Thank you for contributing!

@coltnz

Yeah I agonised over that. Looked at my own uses which were predominately comparing known pairs of dates. Officially on holiday now so might be a week or so before I can finish off.
I think I like the dual selection but are we worried about api bloat, it's getting quite large?

@michaelklishin

It's not getting large. Note that you can have two arities on a function, so you can do

(defn earliest
  ([dates]
    ...)
  ([^DateTime a ^DateTime b]
    ...))

and the variadic case won't be necessary at all.

@coltnz

Ping. Did I resubmit right?

@michaelklishin michaelklishin merged commit 526ad1b into from
@michaelklishin

Looks good @coltnz, thank you. I will take care of a couple of minor/cosmetic things.

@michaelklishin

Done. Thank you for contributing!

@michaelklishin michaelklishin referenced this pull request
Closed

earliest, latest #107

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 46 additions and 0 deletions.
  1. +16 −0 src/clj_time/core.clj
  2. +30 −0 test/clj_time/core_test.clj
View
16 src/clj_time/core.clj
@@ -419,6 +419,22 @@
[^Period period]
(plus (now) period))
+(defn earliest
+ "Returns the earliest of the supplied DateTimes"
+ ([dt1 dt2]
+ (if (pos? (compare dt1 dt2)) dt2 dt1))
+ ([dts]
+ (reduce (fn [dt1 dt2]
+ (if (pos? (compare dt1 dt2)) dt2 dt1)) dts)))
+
+(defn latest
+ "Returns the latest of the supplied DateTimes"
+ ([dt1 dt2]
+ (if (neg? (compare dt1 dt2)) dt2 dt1))
+ ([dts]
+ (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
30 test/clj_time/core_test.clj
@@ -239,6 +239,36 @@
(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)))
+ (is (= d2 (earliest d2 d3)))
+ (is (= d1 (earliest d1 d3)))
+ (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]))
+ (is (= Exception) (earliest 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 (= d2 (latest d1 d2)))
+ (is (= d3 (latest d2 d3)))
+ (is (= d3 (latest d1 d3)))
+ (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]))
+ (is (= Exception) (latest 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)
Something went wrong with that request. Please try again.