/
tz_format.clj
78 lines (66 loc) · 2.34 KB
/
tz_format.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
(ns
^{:doc "Time zone formats for phone-number."
:author "Paweł Wilk"
:added "8.12.4-0"}
phone-number.tz-format
(:refer-clojure :exclude [get])
(:require [phone-number.util :as util])
(:import [java.time.format TextStyle]))
;; Time Zone Formats
(def ^{:added "8.12.4-0"
:tag clojure.lang.PersistentArrayMap}
all
"Map of TextStyle objects to time zone formats (keywords) for time zone formatting."
#::{:id nil
:short TextStyle/SHORT
:narrow TextStyle/NARROW
:full TextStyle/FULL
:short-standalone TextStyle/SHORT_STANDALONE
:narrow-standalone TextStyle/NARROW_STANDALONE
:full-standalone TextStyle/FULL_STANDALONE})
(def ^{:added "8.12.4-0"
:tag clojure.lang.PersistentVector}
all-vec
"Vector of time zone formats (keywords)."
(vec (keys all)))
(def ^{:added "8.12.4-0"
:const true
:tag clojure.lang.Keyword}
default ::id)
(def ^{:added "8.12.4-0"
:tag TextStyle}
default-val (all default))
;; Time Zone IDs
(defn valid?
"Returns true if the given tz-format is valid, false otherwise."
{:added "8.12.4-0" :tag Boolean}
([^clojure.lang.Keyword tz-format]
(contains? all tz-format))
([^clojure.lang.Keyword tz-format
^Boolean use-infer]
(contains? all (util/ns-infer "phone-number.tz-format" tz-format use-infer))))
(defn transform
"For the given `Locale` object and `TextStyle` object renders a string describing a
time zone identifier (given as a string) passed as the first argument. If the style
is nil then the original object passed as a `zone-id` argument is returned."
{:added "8.12.4-0"
:tag String}
[^String zone-id
^java.util.Locale l
^TextStyle style]
(if (nil? style)
zone-id
(.getDisplayName (java.time.ZoneId/of zone-id) style l)))
(defn parse
"Parses a time zone format specification and returns a value that can be supplied to
phone-number functions. If nil is given it returns the default value."
{:added "8.12.4-0" :tag TextStyle}
([^clojure.lang.Keyword k]
(parse k true))
([^clojure.lang.Keyword k
^Boolean use-infer]
(if (nil? k)
default-val
(let [k (util/ns-infer "phone-number.tz-format" k use-infer)]
(assert (valid? k) (str "Time zone format " k " is not valid"))
(all k)))))