Skip to content

Instantly share code, notes, and snippets.

@Deraen
Created September 15, 2016 10:31
Show Gist options
  • Save Deraen/e2f839e3440ff6162fa84e82fb8a92c3 to your computer and use it in GitHub Desktop.
Save Deraen/e2f839e3440ff6162fa84e82fb8a92c3 to your computer and use it in GitHub Desktop.
(defn humanize-duration [ms]
(let [seconds (/ ms 1000)
minutes (/ seconds 60)
hours (/ minutes 60)
days (/ hours 24)
;; From Moment, 400 years = 146097 days = 4800 months
months (/ (* days 4800) 146097)
years (/ months 12)]
(cond
(< seconds 45) [:ss seconds]
(<= minutes 1) [:m]
(< minutes 45) [:mm minutes]
(<= hours 1) [:h]
(< hours 22) [:hh hours]
(<= days 1) [:d]
(< days 26) [:dd days]
(<= months 1) [:M]
(< months 11) [:MM months]
(<= years 1) [:y]
:else [:yy years])))
(defn loc
"Some kind of function to get translation for :duration :mm 5 => 5 minutes ago, :m -> minute ago"
[& ks]
nil)
(defn timeago [_]
(r/with-let [now (r/atom (d/now))
;; update every 1 min
update-interval (js/setInterval (fn [_] (reset! now (d/now))) 60000)]
(fn [{:keys [value]}]
[:span (apply localize :duration (humanize-duration (- @now value))))])
(finally (js/clearInterval update-interval))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment