-
Notifications
You must be signed in to change notification settings - Fork 28
/
timeshift.js
233 lines (196 loc) · 10.2 KB
/
timeshift.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
/*!
* TimeShift.js
*
* Copyright 2013 Mobile Wellness Solutions MWS Ltd, Sampo Juustila
* Released under the MIT license
*/
(function() {
var root = this;
var OriginalDate = root.Date;
var TimeShift;
if (typeof exports !== 'undefined') {
TimeShift = exports;
} else {
TimeShift = root.TimeShift = {};
}
var currentTime = undefined;
var timezoneOffset = new OriginalDate().getTimezoneOffset();
function currentDate() {
var time = TimeShift.getTime();
if (typeof time === 'function') {
time = time();
}
if (time !== undefined) {
return new OriginalDate(time);
} else {
return new OriginalDate();
}
}
function realLocalToUtc(realLocal) {
return new OriginalDate(realLocal.getTime() - realLocal.getTimezoneOffset()*60*1000 + timezoneOffset*60*1000);
}
function utcToLocal(utc) {
return new OriginalDate(utc.getTime() - timezoneOffset*60*1000);
}
function localToUtc(local) {
return new OriginalDate(local.getTime() + timezoneOffset*60*1000);
}
function twoDigit(n) {
if (n < 10) {
return "0" + n;
} else {
return "" + n;
}
}
function timezoneName() {
var zone = "GMT";
var offset = Math.abs(timezoneOffset);
if (timezoneOffset < 0) {
zone = zone + "+";
} else if (timezoneOffset > 0) {
zone = zone + "-";
} else {
return zone;
}
return zone + twoDigit(Math.floor(offset/60)) + twoDigit(offset%60);
}
/**
* Return the current time zone offset in minutes. A value of -60 corresponds to GMT+1,
* +60 to GTM-1. Default value is from new Date().getTimezoneOffset().
*/
TimeShift.getTimezoneOffset = function() {
return timezoneOffset;
}
/**
* Set the time zone offset in minutes. -60 corresponds to GMT+1, +60 to GTM-1.
* Changing this will affect the results also for previously created Date instances.
*/
TimeShift.setTimezoneOffset = function(offset) {
timezoneOffset = offset;
}
/**
* Return the currently overridden time value set by `TimeShift.setTime`, or undefined if
* not overridden.
*/
TimeShift.getTime = function() {
return currentTime;
}
/**
* Set the current time in milliseconds after Jan 1 1970 in UTC time, or a callback
* which returns the same. Setting this to undefined will reset to the real current time.
*/
TimeShift.setTime = function(time) {
currentTime = time;
}
/**
* Access to the original Date constructor.
*/
TimeShift.OriginalDate = OriginalDate;
/**
* Mock implementation of Date.
*/
TimeShift.Date = function() {
// Detect whether we're being called with 'new'
// From http://stackoverflow.com/questions/367768/how-to-detect-if-a-function-is-called-as-constructor
var isConstructor = false;
if (this instanceof TimeShift.Date && !this.__previouslyConstructedByTimeShift) {
isConstructor = true;
this.__previouslyConstructedByTimeShift = true;
}
if (!isConstructor) {
return (new TimeShift.Date()).toString();
}
switch (arguments.length) {
case 0:
this.utc = currentDate();
break;
case 1:
this.utc = new OriginalDate(arguments[0]);
break;
case 2:
this.utc = realLocalToUtc(new OriginalDate(arguments[0], arguments[1]));
break;
case 3:
this.utc = realLocalToUtc(new OriginalDate(arguments[0], arguments[1], arguments[2]));
break;
case 4:
this.utc = realLocalToUtc(new OriginalDate(arguments[0], arguments[1], arguments[2], arguments[3]));
break;
case 5:
this.utc = realLocalToUtc(new OriginalDate(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4]));
break;
case 6:
this.utc = realLocalToUtc(new OriginalDate(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]));
break;
default:
this.utc = realLocalToUtc(new OriginalDate(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6]));
break;
}
}
TimeShift.Date.prototype.getDate = function() { return utcToLocal(this.utc).getUTCDate(); }
TimeShift.Date.prototype.getDay = function() { return utcToLocal(this.utc).getUTCDay(); }
TimeShift.Date.prototype.getFullYear = function() { return utcToLocal(this.utc).getUTCFullYear(); }
TimeShift.Date.prototype.getHours = function() { return utcToLocal(this.utc).getUTCHours(); }
TimeShift.Date.prototype.getMilliseconds = function() { return utcToLocal(this.utc).getUTCMilliseconds(); }
TimeShift.Date.prototype.getMinutes = function() { return utcToLocal(this.utc).getUTCMinutes(); }
TimeShift.Date.prototype.getMonth = function() { return utcToLocal(this.utc).getUTCMonth(); }
TimeShift.Date.prototype.getSeconds = function() { return utcToLocal(this.utc).getUTCSeconds(); }
TimeShift.Date.prototype.getUTCDate = function() { return this.utc.getUTCDate(); }
TimeShift.Date.prototype.getUTCDay = function() { return this.utc.getUTCDay(); }
TimeShift.Date.prototype.getUTCFullYear = function() { return this.utc.getUTCFullYear(); }
TimeShift.Date.prototype.getUTCHours = function() { return this.utc.getUTCHours(); }
TimeShift.Date.prototype.getUTCMilliseconds = function() { return this.utc.getUTCMilliseconds(); }
TimeShift.Date.prototype.getUTCMinutes = function() { return this.utc.getUTCMinutes(); }
TimeShift.Date.prototype.getUTCMonth = function() { return this.utc.getUTCMonth(); }
TimeShift.Date.prototype.getUTCSeconds = function() { return this.utc.getUTCSeconds(); }
TimeShift.Date.prototype.setDate = function() { var d = utcToLocal(this.utc); d.setUTCDate.apply(d, Array.prototype.slice.call(arguments, 0)); this.utc = localToUtc(d); return this.utc.getTime(); }
TimeShift.Date.prototype.setFullYear = function() { var d = utcToLocal(this.utc); d.setUTCFullYear.apply(d, Array.prototype.slice.call(arguments, 0)); this.utc = localToUtc(d); return this.utc.getTime(); }
TimeShift.Date.prototype.setHours = function() { var d = utcToLocal(this.utc); d.setUTCHours.apply(d, Array.prototype.slice.call(arguments, 0)); this.utc = localToUtc(d); return this.utc.getTime(); }
TimeShift.Date.prototype.setMilliseconds = function() { var d = utcToLocal(this.utc); d.setUTCMilliseconds.apply(d, Array.prototype.slice.call(arguments, 0)); this.utc = localToUtc(d); return this.utc.getTime(); }
TimeShift.Date.prototype.setMinutes = function() { var d = utcToLocal(this.utc); d.setUTCMinutes.apply(d, Array.prototype.slice.call(arguments, 0)); this.utc = localToUtc(d); return this.utc.getTime(); }
TimeShift.Date.prototype.setMonth = function() { var d = utcToLocal(this.utc); d.setUTCMonth.apply(d, Array.prototype.slice.call(arguments, 0)); this.utc = localToUtc(d); return this.utc.getTime(); }
TimeShift.Date.prototype.setSeconds = function() { var d = utcToLocal(this.utc); d.setUTCSeconds.apply(d, Array.prototype.slice.call(arguments, 0)); this.utc = localToUtc(d); return this.utc.getTime(); }
TimeShift.Date.prototype.setUTCDate = function() { this.utc.setUTCDate.apply(this.utc, Array.prototype.slice.call(arguments, 0)); return this.utc.getTime(); }
TimeShift.Date.prototype.setUTCFullYear = function() { this.utc.setUTCFullYear.apply(this.utc, Array.prototype.slice.call(arguments, 0)); return this.utc.getTime(); }
TimeShift.Date.prototype.setUTCHours = function() { this.utc.setUTCHours.apply(this.utc, Array.prototype.slice.call(arguments, 0)); return this.utc.getTime(); }
TimeShift.Date.prototype.setUTCMilliseconds = function() { this.utc.setUTCMilliseconds.apply(this.utc, Array.prototype.slice.call(arguments, 0)); return this.utc.getTime(); }
TimeShift.Date.prototype.setUTCMinutes = function() { this.utc.setUTCMinutes.apply(this.utc, Array.prototype.slice.call(arguments, 0)); return this.utc.getTime(); }
TimeShift.Date.prototype.setUTCMonth = function() { this.utc.setUTCMonth.apply(this.utc, Array.prototype.slice.call(arguments, 0)); return this.utc.getTime(); }
TimeShift.Date.prototype.setUTCSeconds = function() { this.utc.setUTCSeconds.apply(this.utc, Array.prototype.slice.call(arguments, 0)); return this.utc.getTime(); }
TimeShift.Date.prototype.getYear = function() { return this.getFullYear() - 1900; }
TimeShift.Date.prototype.setYear = function(v) { return this.setFullYear(v + 1900); }
TimeShift.Date.prototype.getTime = function() { return this.utc.getTime(); }
TimeShift.Date.prototype.setTime = function(v) { return this.utc.setTime(v); }
TimeShift.Date.prototype.getTimezoneOffset = function() { return timezoneOffset; }
TimeShift.Date.prototype.toDateString = function() { return utcToLocal(this.utc).toDateString(); } // Wrong
TimeShift.Date.prototype.toLocaleDateString = function() { return utcToLocal(this.utc).toLocaleDateString(); } // Wrong
TimeShift.Date.prototype.toISOString = function() { return this.utc.toISOString(); }
TimeShift.Date.prototype.toGMTString = function() { return this.utc.toGMTString(); }
TimeShift.Date.prototype.toUTCString = function() { return this.utc.toUTCString(); }
TimeShift.Date.prototype.toString = function() {
var wkdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var d = utcToLocal(this.utc);
// Match browser behavior for invalid dates
if (d.toString() === "Invalid Date") {
return d.toString();
}
// Mon Mar 05 2012 06:07:08 GMT+0500
return wkdays[d.getUTCDay()] + " " + months[d.getUTCMonth()] + " " + twoDigit(d.getUTCDate()) + " " + d.getUTCFullYear() +
" " + twoDigit(d.getUTCHours()) + ":" + twoDigit(d.getUTCMinutes()) + ":" + twoDigit(d.getUTCSeconds()) + " " + timezoneName();
}
TimeShift.Date.prototype.toLocaleString = function() { return this.toString(); } // Wrong
TimeShift.Date.prototype.toLocaleTimeString = function() { return this.toString(); } // Wrong
TimeShift.Date.prototype.toTimeString = function() { return this.toString(); } // Wrong
TimeShift.Date.prototype.toJSON = function() { return this.utc.toJSON(); }
TimeShift.Date.prototype.valueOf = function() { return this.utc.getTime(); }
TimeShift.Date.now = function() { return currentDate().getTime(); }
TimeShift.Date.parse = OriginalDate.parse; // Wrong
TimeShift.Date.UTC = OriginalDate.UTC;
/**
* Helper method that describes a Date object contents.
*/
TimeShift.Date.prototype.desc = function() {
return "utc=" + this.utc.toUTCString() + " local=" + utcToLocal(this.utc).toUTCString() + " offset=" + timezoneOffset;
}
}).call(typeof global !== 'undefined' ? global : this);