-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.js
299 lines (273 loc) · 8.07 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
'use strict';
var assert = require('assert');
var tzdata = require('./lib/tzdata.js');
exports.tzdata = tzdata;
var _Date = null;
exports._Date = Date;
var mockDateOptions = {};
var timezone;
var weekDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'];
var HOUR = 60 * 60 * 1000;
var date_iso_8601_regex = /^\d\d\d\d(-\d\d(-\d\d(T\d\d:\d\d(:\d\d)?(\.\d\d\d)?(\d\d\d)?(Z|[+-]\d\d:?\d\d))?)?)?$/;
var date_with_offset = /^\d\d\d\d-\d\d-\d\d( \d\d:\d\d:\d\d(\.\d\d\d)? )?(Z|(-|\+|)\d\d:\d\d)$/;
var date_rfc_2822_regex = /^\d\d-\w\w\w-\d\d\d\d \d\d:\d\d:\d\d (\+|-)\d\d\d\d$/;
var local_date_regex = /^\d\d\d\d-\d\d-\d\d[T ]\d\d:\d\d(:\d\d(\.\d\d\d)?)?$/;
var local_GMT_regex = /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun), \d\d \w\w\w \d\d\d\d \d\d:\d\d:\d\d GMT$/
function MockDate(param) {
if (arguments.length === 0) {
this.d = new _Date();
} else if (arguments.length === 1) {
if (param instanceof MockDate) {
this.d = new _Date(param.d);
} else if (typeof param === 'string') {
if (param.match(date_iso_8601_regex) ||
param.match(date_with_offset) ||
param.match(date_rfc_2822_regex) ||
param.match(local_GMT_regex) ||
param === ''
) {
this.d = new _Date(param);
} else if (param.match(local_date_regex)) {
this.d = new _Date();
this.fromLocal(new _Date(param.replace(' ', 'T') + 'Z'));
} else if (mockDateOptions.fallbackFn) {
this.d = mockDateOptions.fallbackFn(param);
} else {
assert.ok(false, 'Unhandled date format passed to MockDate constructor: ' + param);
}
} else if (typeof param === 'number' || param === null || param === undefined) {
this.d = new _Date(param);
} else if (mockDateOptions.fallbackFn) {
this.d = mockDateOptions.fallbackFn(param);
} else {
assert.ok(false, 'Unhandled type passed to MockDate constructor: ' + typeof param);
}
} else {
this.d = new _Date();
this.fromLocal(new _Date(_Date.UTC.apply(null, arguments)));
}
}
// eslint-disable-next-line consistent-return
MockDate.prototype.calcTZO = function (ts) {
var data = tzdata[timezone];
assert.ok(data, 'Unsupported timezone: ' + timezone);
ts = (ts || this.d.getTime()) / 1000;
if (Number.isNaN(ts)) {
return NaN;
}
for (var ii = 2; ii < data.transitions.length; ii += 2) {
if (data.transitions[ii] > ts) {
return -data.transitions[ii - 1];
}
}
// note: should never reach here!
assert.ok(false, ts);
};
function passthrough(fn) {
MockDate.prototype[fn] = function () {
var real_date;
if (this instanceof MockDate) {
real_date = this.d;
} else if (this instanceof _Date) {
// console.log calls our prototype to format regular Date objects!
// This should only be hit while debugging MockDate itself though, as
// there should be no _Date objects in user code when using MockDate.
real_date = this;
} else {
assert(false, 'Unexpected object type');
}
return real_date[fn].apply(real_date, arguments);
};
}
function localgetter(fn) {
MockDate.prototype[fn] = function () {
if (Number.isNaN(this.d.getTime())) {
return NaN;
}
var d = new _Date(this.d.getTime() - this.calcTZO() * HOUR);
return d['getUTC' + fn.slice(3)]();
};
}
MockDate.prototype.fromLocal = function (d) {
// From a Date object in the fake-timezone where the returned UTC values are
// meant to be interpreted as local values.
this.d.setTime(d.getTime() + this.calcTZO(d.getTime() + this.calcTZO(d.getTime()) * HOUR) * HOUR);
};
function localsetter(fn) {
MockDate.prototype[fn] = function () {
var d = new _Date(this.d.getTime() - this.calcTZO() * HOUR);
d['setUTC' + fn.slice(3)].apply(d, arguments);
this.fromLocal(d);
return this.getTime();
};
}
[
'getUTCDate',
'getUTCDay',
'getUTCFullYear',
'getUTCHours',
'getUTCMilliseconds',
'getUTCMinutes',
'getUTCMonth',
'getUTCSeconds',
'getTime',
'setTime',
'setUTCDate',
'setUTCFullYear',
'setUTCHours',
'setUTCMilliseconds',
'setUTCMinutes',
'setUTCMonth',
'setUTCSeconds',
'toGMTString',
'toISOString',
'toJSON',
'toUTCString',
'valueOf',
].forEach(passthrough);
[
'getDate',
'getDay',
'getFullYear',
'getHours',
'getMilliseconds',
'getMinutes',
'getMonth',
'getSeconds',
].forEach(localgetter);
[
'setDate',
'setFullYear',
'setHours',
'setMilliseconds',
'setMinutes',
'setMonth',
'setSeconds',
].forEach(localsetter);
MockDate.prototype.getYear = function () {
return this.getFullYear() - 1900;
};
MockDate.prototype.setYear = function (yr) {
if (yr < 1900) {
return this.setFullYear(1900 + yr);
}
return this.setFullYear(yr);
};
MockDate.parse = function (dateString) {
return new MockDate(dateString).getTime();
};
MockDate.prototype.getTimezoneOffset = function () {
if (Number.isNaN(this.d.getTime())) {
return NaN;
}
return this.calcTZO() * 60;
};
MockDate.prototype.toString = function () {
if (this instanceof _Date) {
// someone, like util.inspect, calling Date.prototype.toString.call(foo)
return _Date.prototype.toString.call(this);
}
if (Number.isNaN(this.d.getTime())) {
return new _Date('').toString();
}
var str = [this.d.toISOString() + ' UTC (MockDate: GMT'];
var tzo = -this.calcTZO();
if (tzo < 0) {
str.push('-');
tzo *= -1;
} else {
str.push('+');
}
str.push(Math.floor(tzo).toString().padStart(2, '0'));
tzo -= Math.floor(tzo);
if (tzo) {
str.push(tzo * 60);
} else {
str.push('00');
}
str.push(')');
return str.join('');
};
MockDate.now = function () { return _Date.now() };
MockDate.UTC = function () { return _Date.UTC.apply(_Date, arguments) };
MockDate.prototype.toDateString = function () {
if (Number.isNaN(this.d.getTime())) {
return new _Date('').toDateString();
}
return weekDays[this.getDay()] + ' ' + months[this.getMonth()] + ' ' +
this.getDate().toString().padStart(2, '0') + ' ' + this.getFullYear();
};
MockDate.prototype.toLocaleString = function (locales, options) {
options = Object.assign({ timeZone: timezone }, options);
var time = this.d.getTime();
if (Number.isNaN(time)) {
return new _Date('').toDateString();
}
return new _Date(time).toLocaleString(locales, options);
};
MockDate.prototype.toLocaleDateString = function (locales, options) {
options = Object.assign({ timeZone: timezone }, options);
var time = this.d.getTime();
if (Number.isNaN(time)) {
return new _Date('').toDateString();
}
return new _Date(time).toLocaleDateString(locales, options);
};
MockDate.prototype.toLocaleTimeString = function (locales, options) {
options = Object.assign({ timeZone: timezone }, options);
var time = this.d.getTime();
if (Number.isNaN(time)) {
return new _Date('').toDateString();
}
return new _Date(time).toLocaleTimeString(locales, options);
};
// TODO:
// 'toTimeString',
function options(opts) {
mockDateOptions = opts || {};
}
exports.options = options;
var orig_object_toString;
function mockDateObjectToString() {
if (this instanceof MockDate) {
// Look just like a regular Date to anything doing very low-level Object.prototype.toString calls
// See: https://github.com/Jimbly/timezone-mock/issues/48
return '[object Date]';
}
return orig_object_toString.call(this);
}
function register(new_timezone, glob) {
if (!glob) {
if (typeof window !== 'undefined') {
glob = window;
} else {
glob = global;
}
}
timezone = new_timezone || 'US/Pacific';
if (glob.Date !== MockDate) {
_Date = glob.Date;
exports._Date = glob.Date;
}
glob.Date = MockDate;
if (!orig_object_toString) {
orig_object_toString = Object.prototype.toString;
Object.prototype.toString = mockDateObjectToString;
}
}
exports.register = register;
function unregister(glob) {
if (!glob) {
if (typeof window !== 'undefined') {
glob = window;
} else {
glob = global;
}
}
if (glob.Date === MockDate) {
assert(_Date);
glob.Date = _Date;
}
}
exports.unregister = unregister;