Skip to content

Instantly share code, notes, and snippets.

@orient-man
Last active December 13, 2015 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orient-man/4977668 to your computer and use it in GitHub Desktop.
Save orient-man/4977668 to your computer and use it in GitHub Desktop.
Substitutes built in Date object with our own to support time shifting
/*jslint strict: false */
// beware TOXIC HACK
(function ($) {
// footer contains UpdatePanel with server time
function getApplicationTime() {
var regex = /.*(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2}).*/g,
m = regex.exec($('#ctl00_UpdateLastAsync').text()), i;
for (i = 1; i <= 6; i = i + 1) {
m[i] = parseInt(m[i], 10);
}
return (new Date(m[1], m[2] - 1, m[3], m[4], m[5], m[6], 0)).getTime();
}
function copyProperties(source, target) {
for (property in source) {
if (source.hasOwnProperty(property)) {
target[property] = source[property];
}
}
}
function substituteJavaScriptDate() {
try {
var OriginalDateClass = Date,
appTime = getApplicationTime(),
clientTime = (new Date()).getTime(),
timeDelta = appTime - clientTime,
NewDateClass = function () {
var args = [0, 1, 1, 0, 0, 0, 0], newDate, i;
// workaround: apply(this, arguments) doesn't work on Date constructor
if (arguments.length > 0) {
if (arguments.length === 1) {
// string or ticks
newDate = new OriginalDateClass(arguments[0]);
}
else {
for (i = 0; i < arguments.length; i = i + 1) {
args[i] = arguments[i];
}
newDate = new OriginalDateClass(
args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
}
}
else {
newDate = new OriginalDateClass();
try {
newDate.setTime(newDate.getTime() + timeDelta);
}
catch (e) { }
}
return newDate;
};
copyProperties(Date, NewDateClass);
NewDateClass.prototype.__proto__ = Date;
Date = NewDateClass;
}
catch (e) {
/* no changes */
}
}
$(substituteJavaScriptDate);
} (jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment