Skip to content

Instantly share code, notes, and snippets.

@slyphon
Created May 8, 2010 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slyphon/5b80f2242f719fca5a07 to your computer and use it in GitHub Desktop.
Save slyphon/5b80f2242f719fca5a07 to your computer and use it in GitHub Desktop.
(ns timesugar
(:import [java.sql Timestamp])
(defn now
"returns the current time in milliseconds"
[]
(System/currentTimeMillis))
(defn megabytes
"convert integer n to megabytes"
[n] (* (int n) 1024 1024))
(defn timestamp
"returns a java.sql.Timestamp, with no args, returns the current time
otherwise returns the Timestamp at milliseconds-since-the-epoch"
([] (timestamp (System/currentTimeMillis)))
([millis] (Timestamp. (long millis))))
(defn msec
"returns n seconds in milliseconds
if given a java.sql.Timestamp object, returns the number of milliseconds the
object represents"
[n]
(condp = (class n)
Timestamp (.getTime n)
(* n 1000)))
(def seconds msec)
(defn minutes
"return n minutes in milliseconds"
[n] (msec (* n 60)))
(defn hours
"returns n hours in milliseconds"
[n] (* n (minutes 60)))
(defn days
"returns n days in milliseconds"
[n] (* n (hours 24)))
(defn ago
"returns a long representing the time ms milliseconds ago (in unix time)"
[ms]
(- (now) ms))
(defn from-now
"returns a long representing the time ms milliseconds from now (in unix time)"
[ms]
(+ (now) ms))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment