-
Notifications
You must be signed in to change notification settings - Fork 3
/
DateTimeFormat.elm
173 lines (158 loc) · 6.07 KB
/
DateTimeFormat.elm
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
module Test.DateTimeFormat exposing (all)
import Test exposing (..)
import Expect
import Intl.DateTimeFormat as DateTimeFormat
import Intl.Locale as Locale
import Intl.TimeZone exposing (utc)
import List exposing (map, concat, length)
import String exposing (startsWith)
import Maybe exposing (Maybe(Just), withDefault)
import Date
import Time exposing (second)
all : Test
all =
let
fromLocaleTests =
describe "fromLocale and format"
[ test "English" <|
\() ->
let
format =
DateTimeFormat.format (DateTimeFormat.fromLocale Locale.en)
in
Expect.equal
(format (Date.fromTime (1482683025 * second)))
"12/25/2016"
]
fromOptionsTests =
describe "fromOptions and resolvedOptions"
(map
(\options ->
test ("options " ++ toString options) <|
\() ->
let
resolved =
DateTimeFormat.resolvedOptions (DateTimeFormat.fromOptions options)
in
Expect.true "Expected resolved options to not have something match"
(resolved.weekday
== options.weekday
|| resolved.era
== options.era
|| resolved.year
== options.year
|| resolved.month
== options.month
|| resolved.day
== options.day
|| resolved.hour
== options.hour
|| resolved.minute
== options.minute
|| resolved.second
== options.second
|| resolved.timeZoneName
== options.timeZoneName
)
)
optionCombinations
)
supportedLocalesOfTests =
describe "supportedLocalesOf"
[ test "at least one predefined locale is supported" <|
\() ->
Expect.atLeast 1
(length (DateTimeFormat.supportedLocalesOf predefinedLocales))
, test "not all locales are supported" <|
\() ->
Expect.equal
(DateTimeFormat.supportedLocalesOf
[ withDefault Locale.en (Locale.fromLanguageTag "tlh")
-- Klingon
, withDefault Locale.en (Locale.fromLanguageTag "qya")
-- Elvish
]
)
[]
]
defaultsTests =
describe "defaults"
[ test "end up being m/d/y" <|
\() ->
let
resolved =
DateTimeFormat.fromOptions defaults
|> DateTimeFormat.resolvedOptions
in
Expect.true "mdy"
(resolved.year
== DateTimeFormat.NumericNumber
&& resolved.month
== DateTimeFormat.NumericMonth
&& resolved.day
== DateTimeFormat.NumericNumber
)
]
in
describe "Intl.DateTimeFormat"
[ fromLocaleTests
, fromOptionsTests
, supportedLocalesOfTests
, defaultsTests
]
defaults : DateTimeFormat.Options
defaults =
DateTimeFormat.defaults
predefinedLocales : List Locale.Locale
predefinedLocales =
[ Locale.en
, Locale.zhCN
, Locale.zhTW
, Locale.fr
, Locale.de
, Locale.it
, Locale.ja
, Locale.ko
]
{- A couple commons configurations -}
optionCombinations : List DateTimeFormat.Options
optionCombinations =
[ { defaults
| weekday = DateTimeFormat.LongName
, year = DateTimeFormat.NumericNumber
, month = DateTimeFormat.LongMonth
, day = DateTimeFormat.NumericNumber
, hour = DateTimeFormat.NumericNumber
, minute = DateTimeFormat.TwoDigitNumber
, second = DateTimeFormat.TwoDigitNumber
}
, { defaults
| weekday = DateTimeFormat.NarrowName
, year = DateTimeFormat.NumericNumber
, month = DateTimeFormat.NarrowMonth
, day = DateTimeFormat.NumericNumber
}
, { defaults
| year = DateTimeFormat.NumericNumber
, month = DateTimeFormat.ShortMonth
, day = DateTimeFormat.NumericNumber
}
, { defaults
| year = DateTimeFormat.NumericNumber
, month = DateTimeFormat.LongMonth
}
, { defaults
| month = DateTimeFormat.LongMonth
, day = DateTimeFormat.NumericNumber
}
, { defaults
| hour = DateTimeFormat.NumericNumber
, minute = DateTimeFormat.TwoDigitNumber
, second = DateTimeFormat.TwoDigitNumber
}
, { defaults
| hour12 = Just False
, hour = DateTimeFormat.TwoDigitNumber
, minute = DateTimeFormat.TwoDigitNumber
}
]