javascript - Monkeypatch the JavasScript date object -


i know crazy hack curious anyhow. have environment has wrong system time , cannot set correct time. it's specialized hardware cannot change system time. have service gives correct current time. our issue bunch of ssl , token signing libraries break because getting wrong datetime javascript date object ( since have wrong system time).

what's way monkeypatch date object's constructor can feed correct time initialize subsequent calls date(), date.tostring(), etc... in dependent libraries return our new method returns correct non-system time?

will work?

var olddate = date; date = function(){     return new olddate(specialcalltogetcorrecttime());  } date.prototype = olddate.prototype; 

will work?

no, since not respect arguments given new date function or whether called constructor vs not. forgot fix date.now(). still need right:

date = (function (olddate, oldnow) {     function date(year, month, date, hours, minutes, seconds, ms) {          var res, l = arguments.length;          if (l == 0) {              res = new olddate(date.now());          } else if (l == 1) {              res = new olddate(year); // milliseconds since epoch,          } else {              res = new olddate(                  year,                  month,                  l > 2 ? date : 1,                  l > 3 ? hours : 0,                  l > 4 ? minutes : 0,                  l > 5 ? seconds : 0,                  l > 6 ? ms : 0)          }          if (this instanceof date) {              return res;                           } else {              return res.tostring();          }     }     date.prototype = olddate.prototype; // required instanceof checks     date.now = function() {          return oldnow() + offset; // system time wrong     };     date.parse = olddate.parse;     date.utc = olddate.utc;     return date; })(date, date.now); 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -