Last active
February 24, 2017 06:59
-
-
Save rauhs/3ec9f05f2501bfb40acec35eca3fa73e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| ;; Today at 2:05pm: (either future or past) | |
| (.toInstant | |
| (LocalDateTime/of (LocalDate/now) (LocalTime/of 14 05)) | |
| (ZoneOffset/UTC)) | |
| (defn ms-until+next | |
| "Returns a vector of milliseconds: | |
| 1. The ms until the H:M of today. Negative if in the past. | |
| 2. The ms until the next H:M | |
| - Equal if in the future." | |
| [^LocalTime local-time] | |
| (let [then (.toInstant | |
| (LocalDateTime/of (LocalDate/now) local-time) | |
| (ZoneOffset/UTC)) | |
| now-ms (System/currentTimeMillis) | |
| ms-until (- (inst-ms then) now-ms)] | |
| [ms-until (if (neg? ms-until) | |
| (- (inst-ms (.plus then 1 (ChronoUnit/DAYS))) now-ms) | |
| ms-until)])) | |
| #_(ms-until+next (LocalTime/of 14 3)) | |
| ;; Days since epoch: | |
| (.toEpochDay (.toLocalDate | |
| (.atOffset (Instant/ofEpochMilli some-ms) | |
| (ZoneOffset/UTC)))) | |
| ;; Get the hour of right now: | |
| (.getHour (LocalTime/now (ZoneOffset/UTC))) | |
| ;; d is a Date instance, get next Monday (or today): | |
| (.with | |
| (LocalDate/of (+ 1900 (.getYear d)) | |
| (inc (.getMonth d)) | |
| (.getDate d)) | |
| (TemporalAdjusters/previousOrSame DayOfWeek/MONDAY)) | |
| (defn secs->string | |
| "Converts a seconds to a string using: | |
| - w - week (if move then 45 days) | |
| - d - days | |
| - h - hours | |
| - m - minutes | |
| - s - seconds" | |
| ^String [secs] | |
| (let [d (Duration/of secs ChronoUnit/SECONDS) | |
| days (.toDays d) | |
| d (if (zero? days) d (.minusDays d days)) | |
| [weeks days] (if (< 45 days) [(quot days 7) (rem days 7)] [0 days]) | |
| hours (.toHours d) | |
| d (if (zero? hours) d (.minusHours d hours)) | |
| mins (.toMinutes d) | |
| d (if (zero? mins) d (.minusMinutes d mins)) | |
| secs (.getSeconds d)] | |
| (str/join " " | |
| (sequence | |
| (comp | |
| (map (fn [n s] | |
| (when-not (zero? n) | |
| (str n s)))) | |
| (remove nil?)) | |
| [weeks days hours mins secs] | |
| [\w \d \h \m \s])))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment