Created
August 13, 2014 09:20
-
-
Save emlyn/a81e4f36b866fb2f22d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Doesn't work... | |
;; TODO: allow (truncate n (t/minutes 10)): "2014-06-27 12:34:56.789" -> "2014-06-27 12:30:00.000" | |
(defmacro mutator [method val] | |
`(fn [^MutableDateTime mdt#] (~method mdt# val))) | |
(defn truncate [^DateTime dt units] | |
(loop [^MutableDateTime mdt (.toMutableDateTime dt) | |
[[unit method] & more] [[t/years nil] | |
[t/months (mutator .setMonthOfYear 1)] | |
[t/days (mutator .setDayOfMonth 1)] | |
[t/hours (mutator .setHourOfDay 0)] | |
[t/minutes (mutator .setMinuteOfHour 0)] | |
[t/seconds (mutator .setSecondOfMinute 0)] | |
[nil (mutator .setMillisOfSecond 0)]] | |
zero? false] | |
(when zero? (method mdt)) | |
(if unit | |
(recur mdt more (or zero? (= (unit) units))) | |
(if zero? | |
(.toDateTime mdt) | |
(throw (Exception. "Unrecognised unit")))))) | |
(do ;comment | |
(let [n (t/now)] | |
(println n (truncate n (t/years))) | |
(println n (truncate n (t/months))) | |
(println n (truncate n (t/days))) | |
(println n (truncate n (t/hours))) | |
(println n (truncate n (t/minutes))) | |
(println n (truncate n (t/seconds))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment