Skip to content

Instantly share code, notes, and snippets.

@jplaza
Last active May 4, 2018 19:38
Show Gist options
  • Save jplaza/1f2b2d8ad6e384f739e3c26a42d76b5c to your computer and use it in GitHub Desktop.
Save jplaza/1f2b2d8ad6e384f739e3c26a42d76b5c to your computer and use it in GitHub Desktop.
Convert seconds to a stop watch
(defn strftime [secs]
(format "%02d:%02d:%02d"
(mod (int (/ secs 3600)) 24)
(mod (int (/ secs 60)) 60)
(mod secs 60)))
(defn secs-elapsed-since-midnight []
(let [now (java.time.ZonedDateTime/now)
midnight (.truncatedTo now java.time.temporal.ChronoUnit/DAYS)
duration (java.time.Duration/between midnight now)]
(.getSeconds duration)))
(defn print-clock
[]
(let [t (atom (secs-elapsed-since-midnight))]
(while true
(do
(print (strftime @t))
(flush)
(Thread/sleep 1000)
(print "\b\b\b\b\b\b\b\b")
(flush)
(swap! t inc)))))
(print-clock)
(defn strftime [secs]
(format "%02d:%02d:%02d"
(mod (int (/ secs 3600)) 24)
(mod (int (/ secs 60)) 60)
(mod secs 60)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment