Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/liebke/incanter
Browse files Browse the repository at this point in the history
  • Loading branch information
alexott committed Dec 17, 2009
2 parents 9392e84 + 62edc17 commit 289016b
Showing 1 changed file with 83 additions and 56 deletions.
139 changes: 83 additions & 56 deletions src/main/clojure/incanter/chrono.clj
Expand Up @@ -155,16 +155,21 @@

;;brad's hacks

(defn date> [d]
(let [cal (Calendar/getInstance)]
(.setTime cal d)
(date cal)))
(defn date>
""
([d]
(let [cal (Calendar/getInstance)]
(.setTime cal d)
(date cal))))

;;joda time

(defn time-zone [offset] (DateTimeZone/forOffsetHours offset))
(defn time-zone
""
([offset] (DateTimeZone/forOffsetHours offset)))

(defn joda-date
""
([str-d] (DateTime. str-d))
([y m d h min sec mill zone]
(DateTime. y m d h min sec mill zone)))
Expand Down Expand Up @@ -199,48 +204,63 @@
(valAt [unit] (.invoke this unit))
(equiv [o] (.equals this o)))))

(defn joda-str [d]
(str `(DateTime. ~(str d))))
(defn joda-str
""
([d] (str `(DateTime. ~(str d)))))

(defmethod print-dup org.joda.time.DateTime [d w] (.write w (joda-str d)))

(defn joda-guard [d]
(not (instance? org.joda.time.DateTime d)))
;;TODO: make this stuff monadic
(defn minutes-between [start end]
(if (or (joda-guard start) (joda-guard end))
nil
(.getMinutes (Minutes/minutesBetween start end))))

(defn hours-between [start end]
(if (or (joda-guard start) (joda-guard end))
nil
(.getHours (Hours/hoursBetween start end))))

(defn hours-from [d h]
(.plusHours d h))

(defn minutes-from [d m]
(.plusMinutes d m))

(defn hours-around [r d]
(map #(.plusHours d %) r))

(defn before? [start end]
(.isBefore start end))

(defn valid-range? [[start end]]
(maybe? before? start end))

(defn is-within? [d [s e]]
(.contains (Interval. s e) d))

(defn are-overlapping? [[s e] [s1 e1]]
(if (and (valid-range? [s e]) (valid-range? [s1 e1]))
(letfn [(has-overlap? [start end start1 end1]
(not (nil? (.overlap (Interval. start end) (Interval. start1 end1)))))]
(maybe? has-overlap? s e s1 e1))
false))
(defn joda-guard
""
([d] (not (instance? org.joda.time.DateTime d))))

;;TODO: make this stuff monadic
(defn minutes-between
""
([start end]
(if (or (joda-guard start) (joda-guard end))
nil
(.getMinutes (Minutes/minutesBetween start end)))))

(defn hours-between
""
([start end]
(if (or (joda-guard start) (joda-guard end))
nil
(.getHours (Hours/hoursBetween start end)))))

(defn hours-from
""
([d h] (.plusHours d h)))

(defn minutes-from
""
([d m] (.plusMinutes d m)))

(defn hours-around
""
([r d] (map #(.plusHours d %) r)))

(defn before?
""
([start end] (.isBefore start end)))

(defn valid-range?
""
([[start end]] (maybe? before? start end)))

(defn is-within?
""
([d [s e]] (.contains (Interval. s e) d)))

(defn are-overlapping?
""
([[s e] [s1 e1]]
(if (and (valid-range? [s e]) (valid-range? [s1 e1]))
(letfn [(has-overlap? [start end start1 end1]
(not (nil? (.overlap (Interval. start end) (Interval. start1 end1)))))]
(maybe? has-overlap? s e s1 e1))
false)))

;;todo: find otu why this yields different resutls thatn reading the
;;other joda-str function output back in.
Expand Down Expand Up @@ -280,7 +300,9 @@
([the-date units]
(later the-date 1 units)))

(defn date-time [d t] (later d t :minute))
(defn date-time
""
([d t] (later d t :minute)))

(defn earlier
"Returns a date that is earlier than the-date by amount units.
Expand All @@ -290,13 +312,14 @@
([the-date units]
(later the-date -1 units)))

(defn later? [date-a date-b]
(defn later?
"Is date-a later than date-b?"
(.after (date-a :calendar) (date-b :calendar)))
([date-a date-b]
(.after (date-a :calendar) (date-b :calendar))))

(defn earlier? [date-a date-b]
(defn earlier?
"Is date-a earlier than date-b?"
(.before (date-a :calendar) (date-b :calendar)))
([date-a date-b] (.before (date-a :calendar) (date-b :calendar))))

(defn time-between
"How many units between date-a and date-b? Units defaults to seconds."
Expand Down Expand Up @@ -344,13 +367,17 @@

;;; Formatting and Parsing

(defmacro def-date-format [fname [arg] & body]
`(defmethod format-date ~(keyword (name fname)) [~arg ~'_]
~@body))

(defmacro def-date-parser [fname [arg] & body]
`(defmethod parse-date ~(keyword (name fname)) [~arg ~'_]
~@body))
(defmacro def-date-format
""
([fname [arg] & body]
`(defmethod format-date ~(keyword (name fname)) [~arg ~'_]
~@body)))

(defmacro def-date-parser
""
([fname [arg] & body]
`(defmethod parse-date ~(keyword (name fname)) [~arg ~'_]
~@body)))

;;; Use the normal Java date formats

Expand Down

0 comments on commit 289016b

Please sign in to comment.