-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.clj
121 lines (102 loc) · 2.91 KB
/
parse.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
111
112
113
114
115
116
117
118
119
120
121
(ns jedi-time.parse
(:require [jedi-time.internal :as internal])
(:import (java.time LocalDate LocalDateTime ZonedDateTime
YearMonth LocalTime OffsetDateTime
Instant ZoneId ZoneOffset)
(java.time.format DateTimeFormatter)))
(defn- parser-for
"Returns a 2-arity parser-fn. First one accepts a single CharSequence,
whereas the second one also accepts a DateTimeFormatter."
[x]
(case x
:instant (fn parse*
([^CharSequence x]
(Instant/parse x))
([x _]
(parse* x)))
:year-month (internal/parser-variant YearMonth)
:local-time (internal/parser-variant LocalTime)
:local-date (internal/parser-variant LocalDate)
:local-datetime (internal/parser-variant LocalDateTime)
:zoned-datetime (internal/parser-variant ZonedDateTime)
:offset-datetime (internal/parser-variant OffsetDateTime)
))
;;Fully type-hinted public API:
;;=============================
(let [parse-fn (parser-for :year-month)]
(defn parse-year-month
"Parses the <ym> String into a YearMonth object,
using either the specified, or the default, <formatter>."
(^YearMonth [ym]
(parse-fn ym))
(^YearMonth [ym formatter]
(parse-fn ym formatter)))
)
(let [parse-fn (parser-for :local-time)]
(defn parse-local-time
(^LocalTime [t]
(parse-fn t))
(^LocalTime [t formatter]
(parse-fn t formatter)))
)
(let [parse-fn (parser-for :local-date)]
(defn parse-local-date
(^LocalDate [d]
(parse-fn d))
(^LocalDate [d formatter]
(parse-fn d formatter)))
)
(let [parse-fn (parser-for :local-datetime)]
(defn parse-local-datetime
(^LocalDateTime [dt]
(parse-fn dt))
(^LocalDateTime [dt formatter]
(parse-fn dt formatter)))
)
(let [parse-fn (parser-for :zoned-datetime)]
(defn parse-zoned-datetime
(^ZonedDateTime [dt]
(parse-fn dt))
(^ZonedDateTime [dt formatter]
(parse-fn dt formatter)))
)
(let [parse-fn (parser-for :offset-datetime)]
(defn parse-offset-datetime
(^OffsetDateTime [dt]
(parse-fn dt))
(^OffsetDateTime [dt formatter]
(parse-fn dt formatter)))
)
(defn- fmt-of
[x]
(if (string? x)
(DateTimeFormatter/ofPattern x)
x))
(defn dt-formatter
(^DateTimeFormatter [x]
(DateTimeFormatter/ofPattern x))
(^DateTimeFormatter [x default]
(if (or (nil? x)
(= :iso x)
(= :format/iso x)
(= :format/default x))
(fmt-of default)
(fmt-of x))))
(defn zone-id
^ZoneId [x]
(if (or (nil? x)
(= :system x))
(ZoneId/systemDefault)
(if (string? x)
(ZoneId/of ^String x)
x)))
(defn zone-offset
^ZoneOffset [x ^LocalDateTime ldt]
(if (or (nil? x)
(= :system x))
(-> (ZoneId/systemDefault)
.getRules
(.getOffset ldt))
(if (string? x)
(ZoneOffset/of ^String x)
x)))