-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
224 lines (205 loc) · 5.06 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
'use strict'
const {deepStrictEqual: eql} = require('assert')
const {toDate} = require('date-fns-tz')
const dateFns = require('date-fns')
const {DateTime} = require('luxon')
const lex = require('./lex')
const parse = require('./date-fns')(dateFns)
const parseWithLuxon = require('.')(DateTime)
const resetHours = ['setHours', 0]
const resetMinutes = ['setMinutes', 0]
const resetSeconds = ['setSeconds', 0]
const resetMilliseconds = ['setMilliseconds', 0]
const timeZone = 'Europe/Berlin'
const now = '2019-03-31T01:59:00+01:00'
const tests = [
['12am', [
['setHours', 0],
resetMinutes,
resetSeconds,
resetMilliseconds
], '2019-03-31T00:00:00+01:00'],
['12:01am', [
['setHours', 0],
['setMinutes', 1],
resetSeconds,
resetMilliseconds
], '2019-03-31T00:01:00+01:00'],
['1am', [
['setHours', 1],
resetMinutes,
resetSeconds,
resetMilliseconds
], '2019-03-31T01:00:00+01:00'],
['11:59am', [
['setHours', 11],
['setMinutes', 59],
resetSeconds,
resetMilliseconds
], '2019-03-31T11:59:00+02:00'],
['noon', [
['setHours', 12],
resetMinutes,
resetSeconds,
resetMilliseconds
], '2019-03-31T12:00:00+02:00'],
['12pm', [
['setHours', 12],
resetMinutes,
resetSeconds,
resetMilliseconds
], '2019-03-31T12:00:00+02:00'],
['12:01pm', [
['setHours', 12],
['setMinutes', 1],
resetSeconds,
resetMilliseconds
], '2019-03-31T12:01:00+02:00'],
['11:59pm', [
['setHours', 23],
['setMinutes', 59],
resetSeconds,
resetMilliseconds
], '2019-03-31T23:59:00+02:00'],
['midnight', [
['setHours', 0],
resetMinutes,
resetSeconds,
resetMilliseconds
], '2019-03-31T00:00:00+01:00'],
['tomorrow at 7', [
['addDays', 1],
['setHours', 7],
resetMinutes,
resetSeconds,
resetMilliseconds
], '2019-04-01T07:00:00+02:00'],
['yesterday at 7', [
['subDays', 1],
['setHours', 7],
resetMinutes,
resetSeconds,
resetMilliseconds
], '2019-03-30T07:00:00+01:00'],
['today at 8am', [
['setHours', 8],
resetMinutes,
resetSeconds,
resetMilliseconds
], '2019-03-31T08:00:00+02:00'],
['now', [], now],
['3 days ago', [
['subDays', 3]
], '2019-03-28T01:59:00+01:00'],
['a week ago', [
['subWeeks', 1]
], '2019-03-24T01:59:00+01:00'],
['2 weeks ago', [
['subWeeks', 2]
], '2019-03-17T01:59:00+01:00'],
// todo: `aug 25 2015 5pm`
['aug 25th 2015 5pm', [
['setDate', 25],
['setMonth', 7], // 7 = August
['setYear', 2015],
['setHours', 17],
resetMinutes,
resetSeconds,
resetMilliseconds
], '2015-08-25T17:00:00+02:00'],
['august 25th 2015', [
['setDate', 25],
['setMonth', 7], // 7 = August
['setYear', 2015]
], '2015-08-25T01:59:00+02:00'],
['1st of Jan 2020', [
['setDate', 1],
['setMonth', 0], // 0 = January
['setYear', 2020]
], '2020-01-01T01:59:00+01:00'],
['30th of August', [
['setDate', 30],
['setMonth', 7] // 7 = August
], '2019-08-30T01:59:00+02:00'],
['friday', [
['setDay', 5] // 5 = Friday
], '2019-03-29T01:59:00+01:00'],
['this friday', [
['setDay', 5] // 5 = Friday
], '2019-03-29T01:59:00+01:00'],
// todo: should be 2019-04-05T01:59:00+02:00
['next friday', [
['startOfWeek'],
['addWeeks', 1],
['setDay', 5] // 5 = Friday
], '2019-04-05T00:00:00+02:00'],
// todo: should be 2019-03-22T01:59:00+02:00
['last friday', [
['startOfWeek'],
['subWeeks', 1],
['setDay', 5] // 5 = Friday
], '2019-03-22T00:00:00+01:00'],
['in 12 minutes', [
['addMinutes', 12]
], '2019-03-31T03:11:00+02:00'],
['in 12 min', [
['addMinutes', 12]
], '2019-03-31T03:11:00+02:00'],
['in 2 hours', [
['addHours', 2]
], '2019-03-31T04:59:00+02:00'],
['in 31 hours', [
['addHours', 31]
], '2019-04-01T09:59:00+02:00'],
['in 20 hours 40 minutes', [
['addHours', 20],
['addMinutes', 40]
], '2019-03-31T23:39:00+02:00'],
['in 20 hours and 40 minutes', [
['addHours', 20],
['addMinutes', 40]
], '2019-03-31T23:39:00+02:00'],
['in 20.2h', [
['addHours', 20.2]
], '2019-03-31T23:11:00+02:00'],
// todo: should be 2019-05-05T02:59:00+02:00
['in 5 weeks', [
['addWeeks', 5]
], '2019-05-05T01:59:00+02:00'],
['in 2 years', [
['addYears', 2]
], '2021-03-31T01:59:00+02:00'],
['in 2 years and 5 weeks', [
['addYears', 2],
['addWeeks', 5]
], '2021-05-05T01:59:00+02:00'],
['in 1.5 weeks', [
['addWeeks', 1.5]
], '2019-04-10T13:59:00+02:00']
]
for (const [relative, instructions, iso] of tests) {
console.info(relative)
eql(lex(relative), instructions)
// todo: test `parse` once https://github.com/marnusw/date-fns-tz/issues/31 is fixed
const d = DateTime.fromISO(now).setZone('Europe/Berlin')
const d2 = parseWithLuxon(relative, d)
const actual = d2.toISO({suppressMilliseconds: true})
try {
eql(actual, iso)
} catch (err) {
err.relative = relative
throw err
}
}
// 01:00 in Europe/Berlin: switch non-DST -> DST
// require('luxon')
// .DateTime
// .fromISO('2019-03-31T01:59+01:00')
// .setZone('Europe/Berlin')
// .plus({minutes: 2})
// .toISO()
// -> 2019-03-31T03:01+02:00
const d = toDate('2019-03-31T01:59+01:00', {timeZone})
const d2 = parse('in 2 minutes', d)
eql(d2.toISOString(), '2019-03-31T01:01:00.000Z')
console.info('\nTests successful. ✔︎')