-
Notifications
You must be signed in to change notification settings - Fork 110
/
test.js
206 lines (168 loc) · 7.52 KB
/
test.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import assert from 'power-assert'
import format from '../format'
import utcToZonedTime from '../utcToZonedTime'
import zonedTimeToUtc from '.'
describe('zonedTimeToUtc', function () {
it('returns the UTC time of the date in the time zone for a date input and IANA tz', function () {
var result = zonedTimeToUtc(new Date(2014, 5, 25, 10, 0, 0, 123), 'America/Los_Angeles')
assert.deepEqual(result.toISOString(), '2014-06-25T17:00:00.123Z')
})
it('returns the UTC time of the date in the time zone for a string and IANA tz', function () {
var result = zonedTimeToUtc('2014-06-25T10:00:00.123', 'America/Los_Angeles')
assert.deepEqual(result.toISOString(), '2014-06-25T17:00:00.123Z')
})
it('returns the UTC time of the date near DST changeover with IANA tz', function () {
var result = zonedTimeToUtc('2020-10-03T17:00:00.000', 'Australia/Melbourne')
assert.deepEqual(result.toISOString(), '2020-10-03T07:00:00.000Z')
})
it('returns the UTC time of an ISO8601 string with an IANA tz', function () {
let input = new Date(2021, 11, 4, 15, 0, 15, 0)
let result = zonedTimeToUtc(input.toISOString(), 'America/New_York')
assert.deepEqual(result.toISOString(), '2021-12-04T20:00:15.000Z')
})
it('returns the UTC time of the date for a UTC input', function () {
var result = zonedTimeToUtc(new Date(2014, 5, 25, 10, 0, 0, 123), 'UTC')
assert.deepEqual(result.toISOString(), '2014-06-25T10:00:00.123Z')
})
it('returns the UTC time of the date in the time zone for a date input and tz offset', function () {
var result = zonedTimeToUtc(new Date(2014, 5, 25, 10, 0, 0, 123), '+0400')
assert.deepEqual(result.toISOString(), '2014-06-25T06:00:00.123Z')
})
it('returns the UTC time of the date in the time zone for a string and tz offset', function () {
var result = zonedTimeToUtc('2014-06-25T10:00:00.123', '-02:00')
assert.deepEqual(result.toISOString(), '2014-06-25T12:00:00.123Z')
})
it('returns the UTC time of the date for the Z tz', function () {
var result = zonedTimeToUtc(new Date(2014, 5, 25, 10, 0, 0, 123), 'Z')
assert.deepEqual(result.toISOString(), '2014-06-25T10:00:00.123Z')
})
it('works with years < 100 (Date input)', function () {
var input = new Date(0)
input.setFullYear(99, 0, 1)
var result = zonedTimeToUtc(input, 'Europe/Berlin')
assert.deepEqual(result.toISOString(), '0099-01-01T00:06:32.000Z')
})
it('works with years < 100 (string input)', function () {
var result = zonedTimeToUtc('0099-01-01', 'Europe/Berlin')
assert.deepEqual(result.toISOString(), '0098-12-31T23:06:32.000Z')
})
describe('near DST changeover (AEST to AEDT)', function () {
it('zoned time one day before', function () {
var result = zonedTimeToUtc(
new Date('2020-10-03T17:00:00.000'),
'Australia/Melbourne' // +10 hours
)
assert.deepEqual(result.toISOString(), '2020-10-03T07:00:00.000Z')
})
it('zoned time less than one day before', function () {
var result = zonedTimeToUtc(
new Date('2020-10-03T21:00:00.000'),
'Australia/Melbourne' // +10 hours
)
assert.deepEqual(result.toISOString(), '2020-10-03T11:00:00.000Z')
})
it('zoned time some time before', function () {
var result = zonedTimeToUtc(
new Date('2020-10-03T23:45:00.000'),
'Australia/Melbourne' // +10 hours
)
assert.deepEqual(result.toISOString(), '2020-10-03T13:45:00.000Z')
})
it('zoned time at the stroke of midnight before', function () {
var result = zonedTimeToUtc(
new Date('2020-10-04T00:00:00.000'),
'Australia/Melbourne' // +10 hours
)
assert.deepEqual(result.toISOString(), '2020-10-03T14:00:00.000Z')
})
it('zoned time 1 hour 15 minutes before', function () {
var result = zonedTimeToUtc(
new Date('2020-10-04T00:45:00.000'),
'Australia/Melbourne' // +10 hours
)
assert.deepEqual(result.toISOString(), '2020-10-03T14:45:00.000Z')
})
it('zoned time 15 minutes before', function () {
var result = zonedTimeToUtc(
new Date('2020-10-04T01:45:00.000'),
'Australia/Melbourne' // +10 hours
)
assert.deepEqual(result.toISOString(), '2020-10-03T15:45:00.000Z')
})
it('zoned time 15 minutes after', function () {
var result = zonedTimeToUtc(
new Date('2020-10-04T03:15:00.000'),
'Australia/Melbourne' // +11 hours
)
assert.deepEqual(result.toISOString(), '2020-10-03T16:15:00.000Z')
})
it('zoned time 1 hour 15 minutes after', function () {
var result = zonedTimeToUtc(
new Date('2020-10-04T04:15:00.000'),
'Australia/Melbourne' // +11 hours
)
assert.deepEqual(result.toISOString(), '2020-10-03T17:15:00.000Z')
})
it('zoned time one day after', function () {
var result = zonedTimeToUtc(
new Date('2020-10-04T11:00:00.000'),
'Australia/Melbourne' // +11 hours
)
assert.deepEqual(result.toISOString(), '2020-10-04T00:00:00.000Z')
})
it('accepts the Etc/GMT+n time zone format', function () {
var result1 = zonedTimeToUtc('2019-11-26T10:00:00', 'America/Chicago')
var result2 = zonedTimeToUtc('2019-11-26T10:00:00', 'Etc/GMT+6')
assert.deepEqual(result1, result2)
})
})
describe('zonedTimeToUtc and utcToZonedTime are inverse functions', function () {
it('date strings without a time zone specifier', function () {
var timeZone = 'America/Chicago'
var input = '2019-11-26T10:00:00'
var result = utcToZonedTime(zonedTimeToUtc(input, timeZone), timeZone)
assert.deepEqual(format(result, "yyyy-MM-dd'T'HH:mm:ss"), input)
})
it('a Date instance', function () {
var timeZone = 'Etc/GMT+6'
var input = new Date('2019-11-26T10:00:00Z')
var result = utcToZonedTime(zonedTimeToUtc(input, timeZone), timeZone)
assert.deepEqual(result, input)
})
it('date string with Z time zone specifier', function () {
var timeZone = 'Europe/Paris'
var input = '2019-11-26T10:00:00Z'
var result = utcToZonedTime(zonedTimeToUtc(input, timeZone), timeZone)
assert.deepEqual(result, new Date('2019-11-26T10:00:00Z'))
})
it('date string with a positive time zone offset specifier', function () {
var timeZone = 'Australia/Melbourne'
var input = '2019-11-26T10:00:00+02:00'
var result = utcToZonedTime(zonedTimeToUtc(input, timeZone), timeZone)
assert.deepEqual(result, new Date('2019-11-26T10:00:00+02:00'))
})
it('when the time zone is UTC', function () {
var timeZone = 'UTC'
var input = '2019-11-26T10:00:00Z'
var result = utcToZonedTime(zonedTimeToUtc(input, timeZone), timeZone)
assert.deepEqual(result, new Date('2019-11-26T10:00:00Z'))
})
})
describe('invalid date and time zone handling', function () {
it('returns an invalid date when the date string is invalid without tz info', function () {
var result = zonedTimeToUtc('2020-01-01T25:00:00.000', 'Europe/London')
assert(result instanceof Date)
assert(isNaN(result))
})
it('returns an invalid date when the date string is invalid with tz info', function () {
var result = zonedTimeToUtc('2020-01-01T25:00:00.000Z', 'Europe/London')
assert(result instanceof Date)
assert(isNaN(result))
})
it('returns an invalid date when the time zone is invalid', function () {
var result = zonedTimeToUtc('2020-01-01T12:00:00.000Z', 'bad/timezone')
assert(result instanceof Date)
assert(isNaN(result))
})
})
})