Skip to content

Instantly share code, notes, and snippets.

@mtimkovich

mtimkovich/fzcl.clj

Last active Jul 13, 2020
Embed
What would you like to do?
Fuzzy Clock in Clojure
(def words {
1 "one"
2 "two"
3 "three"
4 "four"
5 "five"
6 "six"
7 "seven"
8 "eight"
9 "nine"
10 "ten"
11 "eleven"
12 "twelve"
15 "quarter"
20 "twenty"
30 "half"})
(defn round-time [hours minutes]
(let [rounded (* (int (/ (mod (+ minutes 2) 60) 5)) 5)]
(cond
(> rounded 35) [(inc hours) (- 60 rounded)]
(or (= rounded 25)
(= rounded 35)) [hours 30]
(and (zero? rounded)
(> minutes rounded)) [(inc hours) rounded]
:else [hours rounded])))
(defn fzcl [hours minutes]
(let [[hours rounded] (round-time hours minutes)
glue (if (> rounded minutes) "past" "til")
hour (get words (if (> hours 12)
(- hours 12)
hours))]
(if (zero? rounded)
(print-str hour "o'clock")
(print-str (get words rounded) glue hour))))
(let [now (java.time.LocalTime/now)
hours (.getHour now)
minutes (.getMinute now)]
(println (fzcl hours minutes)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment