-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
187 lines (155 loc) · 4.38 KB
/
index.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
/**
* Inspired by Time keeper - EEasy testing of time-dependent code.
*
* Veselin Todorov <hi@vesln.com>
* MIT License.
*/
const NativeDate = Date;
const nativeGetTimezoneOffset = NativeDate.prototype.getTimezoneOffset;
let freezedAt = null;
let traveledTo = null;
let started = null;
let iana = null;
function FakeDate(...args) {
const length = args.length;
if (!length) {
if (freezedAt) args = [freezedAt];
else if (traveledTo) args = [time()];
}
const dt = instantiate(NativeDate, args);
Object.defineProperty(dt, 'getTimezoneOffset', {
enumerable: false,
value: function getTimezoneOffset() {
const curr = nativeGetTimezoneOffset.call(this);
if (!iana) return curr;
const tz = new TimeZoneTraveller(iana).getTime(this);
return Math.round((tz - this.getTime()) / 60000) + curr;
},
});
return dt;
}
FakeDate.UTC = NativeDate.UTC;
FakeDate.parse = NativeDate.parse;
FakeDate.prototype = NativeDate.prototype;
FakeDate.now = function fakeNow() {
if (freezedAt) return freezedAt.getTime();
return time();
};
export function freeze(...args) {
useFakeDate();
freezedAt = instantiate(Date, args);
if (isNaN(freezedAt)) {
reset();
throw new TypeError('Chronokinesis cannot freeze to invalid date, check your arguments. Chronokinesis is reset');
}
return freezedAt;
}
export function defrost() {
freezedAt = null;
}
export function travel(...args) {
useFakeDate();
const travelToDate = instantiate(Date, args);
if (isNaN(travelToDate)) {
reset();
throw new TypeError('Chronokinesis cannot travel to invalid date, check your arguments. Chronokinesis is reset');
}
traveledTo = travelToDate.getTime();
started = NativeDate.now();
if (freezedAt) {
freezedAt = travelToDate;
}
return travelToDate;
}
export function reset() {
useNativeDate();
freezedAt = null;
started = null;
traveledTo = null;
iana = null;
}
export function isKeepingTime() {
return Date === FakeDate;
}
export function TimeZoneTraveller(timeZone) {
this.timeZone = timeZone;
const options = {
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
hour12: false,
};
this.localFormatter = Intl.DateTimeFormat('UTC', options);
this.timeZoneFormatter = Intl.DateTimeFormat('UTC', {
timeZone,
...options,
});
}
TimeZoneTraveller.prototype.defrost = defrost;
TimeZoneTraveller.prototype.reset = reset;
TimeZoneTraveller.prototype.isKeepingTime = isKeepingTime;
TimeZoneTraveller.prototype.getTime = function timeZoneGetTime(...args) {
const realDate = instantiate(Date, args);
const tz = new NativeDate(toUTC(this.timeZoneFormatter, realDate));
if (!args.length) return tz.getTime();
const currentTz = new NativeDate(toUTC(this.localFormatter, realDate));
return realDate.getTime() + currentTz.getTime() - tz.getTime();
};
TimeZoneTraveller.prototype.freeze = function freezeInTimezone(...args) {
if (!args.length && iana === this.timeZone) return freeze();
iana = this.timeZone;
return freeze(this.getTime(...args));
};
TimeZoneTraveller.prototype.travel = function timeZoneTravel(...args) {
if (!args.length && iana === this.timeZone) return travel();
iana = this.timeZone;
return travel(this.getTime(...args));
};
export function timezone(timeZone, ...args) {
const tz = new TimeZoneTraveller(timeZone);
tz.travel(...args);
return tz;
}
function useFakeDate() {
Date = FakeDate;
}
function useNativeDate() {
Date = NativeDate;
}
function time() {
return traveledTo + (NativeDate.now() - started);
}
function instantiate(type, args) {
const ctorArgs = args.slice();
ctorArgs.unshift(null);
return new (Function.prototype.bind.apply(type, ctorArgs))();
}
function toUTC(formatter, dt) {
let year, month, day, hour, minute, second;
for (const { type, value } of formatter.formatToParts(dt)) {
switch (type) {
case 'year':
year = parseInt(value);
break;
case 'month':
month = parseInt(value) - 1;
break;
case 'day':
day = parseInt(value);
break;
case 'hour':
hour = parseInt(value) % 24;
break;
case 'minute':
minute = parseInt(value);
break;
case 'second':
second = parseInt(value);
break;
}
}
return NativeDate.UTC(year, month, day, hour, minute, second, dt.getMilliseconds());
}