-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathgetUnixTime.test.js
176 lines (162 loc) · 4.2 KB
/
getUnixTime.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
/* global beforeAll, it, expect */
import { findTimeZone, getUnixTime } from '../src/index'
let berlin
beforeAll(() => {
berlin = findTimeZone('Europe/Berlin')
})
it('is exported as a function', () => {
expect(typeof getUnixTime === 'function').toBeTruthy()
})
it('converts the time object to the correct UNIX time', () => {
const berlinTime = {
year: 2018,
month: 1,
day: 2,
hours: 10,
minutes: 30,
seconds: 15,
milliseconds: 234,
zone: {
abbreviation: 'CET',
offset: -60
}
}
const unixTime = getUnixTime(berlinTime)
expect(typeof unixTime === 'number').toBeTruthy()
const epoch = Date.UTC(2018, 0, 2, 9, 30, 15, 234)
expect(unixTime).toEqual(epoch)
})
it('accepts an explicit time zone as a parameter', () => {
const berlinTime = {
year: 2018,
month: 1,
day: 2,
hours: 10,
minutes: 30,
seconds: 15,
milliseconds: 234
}
const unixTime = getUnixTime(berlinTime, berlin)
expect(typeof unixTime === 'number').toBeTruthy()
const epoch = Date.UTC(2018, 0, 2, 9, 30, 15, 234)
expect(unixTime).toEqual(epoch)
})
it('seconds and milliseconds are optional in the time object', () => {
const berlinTime = {
year: 2018,
month: 1,
day: 2,
hours: 10,
minutes: 30,
zone: {
abbreviation: 'CET',
offset: -60
}
}
const unixTime = getUnixTime(berlinTime)
expect(typeof unixTime === 'number').toBeTruthy()
const epoch = Date.UTC(2018, 0, 2, 9, 30)
expect(unixTime).toEqual(epoch)
})
it('recognizes daylight-saving time', () => {
const berlinTime = {
year: 2018,
month: 7,
day: 2,
hours: 11,
minutes: 30,
zone: {
abbreviation: 'CEST',
offset: -120
}
}
const unixTime = getUnixTime(berlinTime)
expect(typeof unixTime === 'number').toBeTruthy()
const epoch = Date.UTC(2018, 6, 2, 9, 30)
expect(unixTime).toEqual(epoch)
})
it('recognizes daylight-saving time switch point of explicit timezone', () => {
// This test depends on what local TZ it is being executed in!!!
// The local TZ has to be at least 2 hours closer to UTC than Melbourne
// The TZ where this has been confirmed was UTC+3 (Europe/Sofia)
// This test shall fail without the fix because the UTC moment is after the DST switch and the target moment is before that
const melbourneTime = {
year: 2023,
month: 10,
day: 1,
hours: 0,
minutes: 0
}
const unixTime = getUnixTime(melbourneTime, findTimeZone('Australia/Melbourne'))
expect(typeof unixTime === 'number').toBeTruthy()
const epoch = Date.UTC(2023, 8, 30, 14, 0)
expect(unixTime).toEqual(epoch)
})
it('checks, that other time zone is specified, if required', () => {
const berlinTime = {
year: 2018,
month: 7,
day: 2,
hours: 11,
minutes: 30
}
expect(() => getUnixTime(berlinTime)).toThrow()
})
it('checks, that only one time zone is requested to convert from', () => {
const berlinTime = {
year: 2018,
month: 7,
day: 2,
hours: 11,
minutes: 30,
zone: {
abbreviation: 'CEST',
offset: -120
}
}
expect(() => getUnixTime(berlinTime, berlin)).toThrow()
})
it('returns the epoch, that only one time zone is requested to convert from', () => {
const berlinTime = {
year: 2018,
month: 7,
day: 2,
hours: 11,
minutes: 30,
epoch: 1530523800000,
zone: {
abbreviation: 'CEST',
offset: -120
}
}
const unixTime = getUnixTime(berlinTime)
expect(typeof unixTime === 'number').toBeTruthy()
const epoch = Date.UTC(2018, 6, 2, 9, 30)
expect(unixTime).toEqual(epoch)
})
it('checks, that other time zone is not requested if epoch is included', () => {
const berlinTime = {
year: 2018,
month: 7,
day: 2,
hours: 11,
minutes: 30,
epoch: 1530523800000
}
expect(() => getUnixTime(berlinTime, berlin)).toThrow()
})
it('if time (hours, minutes and seconds) is not provided, defaults to midnight', () => {
const berlinTime = {
year: 2018,
month: 7,
day: 2,
zone: {
abbreviation: 'CEST',
offset: -120
}
}
const unixTime = getUnixTime(berlinTime)
expect(typeof unixTime === 'number').toBeTruthy()
const epoch = Date.UTC(2018, 6, 1, 22, 0)
expect(unixTime).toEqual(epoch)
})