-
Notifications
You must be signed in to change notification settings - Fork 4
/
db.clj
72 lines (61 loc) · 2.03 KB
/
db.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(ns kameleon.db
(:use [korma.core :exclude [update]]
[korma.db])
(:require [clj-time.coerce :as tc]
[clj-time.core :as t]
[clj-time.format :as tf]
[clojure.string :as string])
(:import [java.sql Timestamp]))
(defn ->enum-val
[val]
(raw (str \' val \')))
(def ^:private timestamp-parser
(tf/formatter (t/default-time-zone)
"EEE MMM dd YYYY HH:mm:ss 'GMT'Z"
"YYYY MMM dd HH:mm:ss"
"YYYY-MM-dd-HH-mm-ss.SSS"
"YYYY-MM-dd HH:mm:ss.SSS"
"YYYY-MM-dd'T'HH:mm:ss.SSSZ"))
(defn- strip-time-zone
"Removes the time zone abbreviation from a date timestamp."
[s]
(string/replace s #"\s*\(\w+\)\s*$" ""))
(defn- parse-timestamp
"Parses a timestamp in one of the accepted formats, returning the number of milliseconds
since the epoch."
[s]
(.getMillis (tf/parse timestamp-parser (strip-time-zone s))))
(defn millis-from-str
"Parses a string representation of a timestamp."
[s]
(assert (or (nil? s) (string? s)))
(cond (or (string/blank? s) (= "0" s)) nil
(re-matches #"\d+" s) (Long/parseLong s)
:else (parse-timestamp s)))
(defn timestamp-from-millis
"Converts the number of milliseconds since the epoch to a timestamp."
[millis]
(when-not (nil? millis)
(Timestamp. millis)))
(defn timestamp-from-str
"Parses a string representation of a timestamp."
[s]
(timestamp-from-millis (millis-from-str s)))
(defn millis-from-timestamp
"Converts a timestamp to the number of milliseconds since the epoch."
[timestamp]
(when-not (nil? timestamp)
(.getTime timestamp)))
(defn now
"Returns a timestamp representing the current date and time."
[]
(Timestamp. (System/currentTimeMillis)))
(defn timestamp-str
"Returns a string containing the number of milliseconds since the epoch for a timestamp."
[timestamp]
(when-not (nil? timestamp)
(str (.getTime timestamp))))
(defn now-str
"Returns a string containing the current number of milliseconds since the epoch."
[]
(str (System/currentTimeMillis)))