Permalink
Browse files

Add timeago to clj util

  • Loading branch information...
1 parent a3f6356 commit 9502bc3a0c0faa8a41161b87b3ba4881b7d870cc @zk committed Oct 20, 2013
Showing with 25 additions and 1 deletion.
  1. +25 −1 src/clj/nsfw/util.clj
View
@@ -213,4 +213,28 @@
(.printStackTrace exc pw)
(.toString sw)))
-(defn now [] (System/currentTimeMillis))
+(defn now [] (System/currentTimeMillis))
+
+(defn ms [date]
+ (cond
+ (= java.util.Date (class date)) (.getTime date)
+ :else (.getMillis date)))
+
+(defn timeago [date-or-ms]
+ (let [ms (if (number? date-or-ms)
+ (- (now) date-or-ms)
+ (- (now) (ms date-or-ms)))
+ s (/ ms 1000)
+ m (/ s 60)
+ h (/ m 60)
+ d (/ h 24)
+ y (/ d 365)]
+ (cond
+ (< s 60) "less than a minute"
+ (< m 2) "1 minute"
+ (< h 1) (str (int m) " minutes")
+ (< h 2) "1 hour"
+ (< d 1) (str (int h) " hours")
+ (< d 2) "1 day"
+ (< y 1) (str (int d) " days")
+ :else "over a year")))

0 comments on commit 9502bc3

Please sign in to comment.