forked from Jimbly/timezone-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimezoneMock.js
352 lines (317 loc) · 9.57 KB
/
TimezoneMock.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
'use strict';
var _typeof =
typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol'
? function(obj) {
return typeof obj;
}
: function(obj) {
return obj &&
typeof Symbol === 'function' &&
obj.constructor === Symbol &&
obj !== Symbol.prototype
? 'symbol'
: typeof obj;
}; /* eslint func-names:"off" */
/* eslint no-case-declarations:"off" */
/* eslint no-console:"off" */
/* eslint no-global-assign:"off" */
/* eslint no-underscore-dangle:"off" */
/* eslint no-unused-expressions:"off" */
/* eslint prefer-rest-params:"off" */
/* global define */
var _momentTimezone = require('moment-timezone');
var moment = _interopRequireWildcard(_momentTimezone);
function _interopRequireWildcard(obj) {
if (obj && obj.__esModule) {
return obj;
} else {
var newObj = {};
if (obj != null) {
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key))
newObj[key] = obj[key];
}
}
newObj.default = obj;
return newObj;
}
}
(function(name, definition) {
if (typeof define === 'function' && _typeof(define.amd) === 'object') {
// AMD
define(definition);
} else if (typeof module !== 'undefined' && module.exports) {
// Node
module.exports = definition();
} else {
// Browser
this[name] = definition;
}
})('TimezoneMock', function() {
// Keep reference to original Date object
var _Date = Date;
// Number of milliseconds in an hour
var HOUR = 60 * 1000;
// Current date/time and timezone
var timezone = null;
var now = null;
// Regular expressions to match constructor strings
var REGEX = {
ISO_8601: /^\d\d\d\d(-\d\d(-\d\d(T\d\d:\d\d:\d\d(\.\d\d\d)?Z)?)?)?$/,
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)$/,
RFC_2822: /^\d\d-\w\w\w-\d\d\d\d \d\d:\d\d:\d\d (\+|-)\d\d\d\d$/,
LOCAL_DATE: /^\d\d\d\d-\d\d-\d\d[T ]\d\d:\d\d:\d\d(\.\d\d\d)?$/,
};
var DEBUG = false;
function isDefined(value) {
return typeof value !== 'undefined' && value !== undefined;
}
// Constructor function
function TimezoneMock() {
DEBUG &&
console.log(
'**** TimezoneMock:constructor - args: ' + JSON.stringify(arguments)
);
DEBUG && console.log('**** TimezoneMock:constructor - now: ' + now);
DEBUG &&
console.log('**** TimezoneMock:constructor - timezone ' + timezone);
switch (arguments.length) {
case 0:
if (now !== null) {
DEBUG &&
console.log(
'**** TimezoneMock:constructor - using existing date ' +
now.getTime()
);
this.date = now;
} else {
DEBUG &&
console.log('**** TimezoneMock:constructor - creating new date');
this.date = new _Date();
}
break;
case 1:
var param = arguments[0];
if (param instanceof TimezoneMock) {
this.date = new _Date(param.date);
} else if (typeof param === 'string') {
if (
param.match(REGEX.ISO_8601) ||
param.match(REGEX.DATE_WITH_OFFSET) ||
param.match(REGEX.RFC_2822)
) {
DEBUG &&
console.log(
'**** TimezoneMock:constructor - creating date from string ' +
param
);
this.date = new _Date(param);
} else if (param.match(REGEX.LOCAL_DATE)) {
DEBUG &&
console.log(
'**** TimezoneMock:constructor - creating date from local date'
);
this.date = new _Date();
DEBUG &&
console.log(
'**** TimezoneMock:constructor - local date created as ' +
this.date.toISOString()
);
this.fromLocal(new _Date(param.replace(' ', 'T') + 'Z'));
DEBUG &&
console.log(
'**** TimezoneMock:constructor - local date adjusted to ' +
this.date.toISOString()
);
} else {
throw new TypeError(
'Unhandled date format ' +
param +
' passed to TimezoneMock constructor'
);
}
} else if (typeof param === 'number') {
DEBUG &&
console.log(
'**** TimezoneMock:constructor - creating date from number ' +
param
);
this.date = new _Date(param);
} else {
throw new TypeError(
'Unhandled type ' +
(typeof param === 'undefined' ? 'undefined' : _typeof(param)) +
' passed to TimezoneMock constructor'
);
}
break;
default:
DEBUG &&
console.log(
'**** TimezoneMock:constructor - creating date from multiple arguments'
);
var y = isDefined(arguments[0]) ? arguments[0] : 1970;
var m = isDefined(arguments[1]) ? arguments[1] : 0;
var d = isDefined(arguments[2]) ? arguments[2] : 1;
var h = isDefined(arguments[3]) ? arguments[3] : 0;
var M = isDefined(arguments[4]) ? arguments[4] : 0;
var s = isDefined(arguments[5]) ? arguments[5] : 0;
var ms = isDefined(arguments[6]) ? arguments[6] : 0;
this.date = new _Date(y, m, d, h, M, s, ms);
this.fromLocal(new _Date(_Date.UTC.apply(null, arguments)));
}
return this;
}
// Returns the timezone offset for any given date/time in the current timezone
function tzOffset(timestamp) {
var result = void 0;
if (isDefined(timestamp)) {
result = moment.tz.zone(timezone).parse(timestamp);
DEBUG &&
console.log(
'**** TimezoneMock:tzOffset - offset for timestamp ' +
timestamp +
' in ' +
timezone +
' is ' +
result
);
} else {
result = moment.tz.zone(timezone).parse(this.date.getTime());
DEBUG &&
console.log(
'**** TimezoneMock:tzOffset - offset for this.date ' +
this.date.getTime() +
' is ' +
result
);
}
return result;
}
TimezoneMock.prototype._tzOffset = tzOffset;
// Construct methods which use the original Date methods
function passthrough(fn) {
TimezoneMock.prototype[fn] = function() {
var _realdate;
var realdate = void 0;
if (this instanceof TimezoneMock) {
realdate = this.date;
} else if (this instanceof _Date) {
realdate = this;
} else {
throw new TypeError('Unexpected object type');
}
return (_realdate = realdate)[fn].apply(_realdate, arguments);
};
}
// Construct getters which adjust results to take account of local system time
function localgetter(fn) {
TimezoneMock.prototype[fn] = function() {
DEBUG &&
console.log(
'**** TimezoneMock.prototype.' +
fn +
' - date: ' +
this.date.getTime() +
' tzoffset: ' +
this._tzOffset()
);
var date = new _Date(
this.date.getTime() - this._tzOffset(this.date.getTime()) * HOUR
);
return date['getUTC' + fn.slice(3)]();
};
}
// Adjusts the date to take account of local system time
TimezoneMock.prototype.fromLocal = function(date) {
this.date.setTime(
date.getTime() +
this._tzOffset(date.getTime() + this._tzOffset(date.getTime()) * HOUR) *
HOUR
);
};
// Construct setters which adjust results to take account of local system time
function localsetter(fn) {
TimezoneMock.prototype[fn] = function() {
var date = new _Date(this.date.getTime() - this._tzOffset() * HOUR);
date['setUTC' + fn.slice(3)].apply(date, arguments);
this.fromLocal(date);
return this;
};
}
[
'getUTCDate',
'getUTCDay',
'getUTCFullYear',
'getUTCHours',
'getUTCMilliseconds',
'getUTCMinutes',
'getUTCMonth',
'getUTCSeconds',
'getTime',
'getTimezoneOffset',
'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);
// TimezoneMock.prototype.getTimezoneOffset =
// TimezoneMock.prototype._tzOffset;
TimezoneMock.prototype.toString = function() {
if (this instanceof _Date) {
return _Date.prototype.toString.call(this);
}
return this.date ? this.date.toISOString() : null; // TBC - need to add GMT+/-xxxx suffix
};
TimezoneMock.prototype.toLocaleString = TimezoneMock.prototype.toString;
TimezoneMock.now = _Date.now;
TimezoneMock.UTC = _Date.UTC;
function set(date) {
var tz =
arguments.length > 1 && arguments[1] !== undefined
? arguments[1]
: 'Etc/UTC';
timezone = isDefined(tz) ? tz : moment.tz.guess();
if (timezone === null) {
timezone = 'Etc/UTC';
}
Date = TimezoneMock;
now = new _Date(date);
DEBUG &&
console.log(
'**** TimezoneMock:set - now: ' + now + ' timezone: ' + timezone
);
}
function reset() {
Date = _Date;
}
return { set: set, reset: reset, _Date: _Date };
});