-
-
Save slyphon/5b80f2242f719fca5a07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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