/
episode_007.clj
110 lines (91 loc) · 2.17 KB
/
episode_007.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
(ns episode-007
(:import (java.util Date Calendar GregorianCalendar)
(java.sql Timestamp)
(org.joda.time DateTime DateTime$Property DateTimeZone
Minutes Hours Period Interval)
(org.joda.time.format ISODateTimeFormat DateTimeFormatter)))
;;----------------------
;; Dispatch Fn
;;----------------------
(defn- to-ms-dispatch
[& params]
(let [lead-param (first params)]
(cond
(empty? params) ::empty
(nil? lead-param) ::nil
true (class lead-param))))
;;---------------------
;; Convert to Long
;;---------------------
(defmulti to-ms to-ms-dispatch)
(defmethod to-ms Long
[& params]
(first params))
(defmethod to-ms Long
[l]
l)
(defmethod to-ms Calendar
[c]
(to-ms (.getTime c)))
(defmethod to-ms Date
[d]
(.getTime d))
(defmethod to-ms Timestamp
[ts]
(.getTime ts))
(defmethod to-ms ::empty
[& params]
(to-ms (Date. )))
(defmethod to-ms ::nil
[& params]
nil)
;;----------------------
;; Convert to Type
;;----------------------
(defn date [& params]
(Date. (apply to-ms params)))
(defn greg-cal [& params]
(doto (GregorianCalendar. )
(.setTime (apply date params))))
(defn sql-ts [& params]
(Timestamp. (apply to-ms params)))
;;---------------------
;; Usages
;;---------------------
(defn compare-time
[a b]
(.compareTo (date a) (date b)))
(defn before?
"Tests to determine if time a is before time b"
[a b]
(= (compare-time a b) -1))
(defn after?
"Tests to determine if time a is after time b"
[a b]
(= (compare-time a b) 1))
(defn some-db-fn
[t & other-stuff]
(update-db {:updated-at (sql-ts t)
:more-stuff other-stuff}))
;;---------------------
;; Extend
;;---------------------
(defmethod to-ms DateTime
[& params]
(.getMillis (first params)))
(defn joda [& params]
(DateTime. (apply to-ms params)))
(defmethod to-ms ::map
[& params]
(let [default-map {:year 2000
:month 1
:day 1
:hour 0
:minute 0
:second 0
:ms 0}
input-map (first params)
resulting-map (merge default-map input-map)
[y mo d h mi s ms] ((juxt :year :month :day :hour :minute :second :ms)
resulting-map)]
(to-ms (DateTime. y mo d h mi s ms))))