replace UTCDate with ISODate
This commit is contained in:
@@ -252,6 +252,13 @@ const StringData _jscode_raw_utils =
|
||||
"return this.replace(/\\s+$/,\"\");\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"Number.prototype.zeroPad = function(width) {\n"
|
||||
"var str = this + '';\n"
|
||||
"while (str.length < width)\n"
|
||||
"str = '0' + str;\n"
|
||||
"return str;\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"Date.timeFunc = function( theFunc , numTimes ){\n"
|
||||
"\n"
|
||||
"var start = new Date();\n"
|
||||
@@ -265,34 +272,68 @@ const StringData _jscode_raw_utils =
|
||||
"}\n"
|
||||
"\n"
|
||||
"Date.prototype.tojson = function(){\n"
|
||||
"var year = this.getUTCFullYear();\n"
|
||||
"var month = this.getUTCMonth() + 1; // js is stupid\n"
|
||||
"var date = this.getUTCDate();\n"
|
||||
"var hour = this.getUTCHours();\n"
|
||||
"var minute = this.getUTCMinutes();\n"
|
||||
"var sec = this.getUTCSeconds() + (this.getUTCMilliseconds()/1000);\n"
|
||||
"\n"
|
||||
"return 'UTCDate('+year+', '+month+', '+date+', '+hour+', '+minute+', '+sec+')';\n"
|
||||
"var UTC = Date.printAsUTC ? 'UTC' : '';\n"
|
||||
"\n"
|
||||
"var year = this['get'+UTC+'FullYear']().zeroPad(4);\n"
|
||||
"var month = (this['get'+UTC+'Month']() + 1).zeroPad(2);\n"
|
||||
"var date = this['get'+UTC+'Date']().zeroPad(2);\n"
|
||||
"var hour = this['get'+UTC+'Hours']().zeroPad(2);\n"
|
||||
"var minute = this['get'+UTC+'Minutes']().zeroPad(2);\n"
|
||||
"var sec = this['get'+UTC+'Seconds']().zeroPad(2)\n"
|
||||
"\n"
|
||||
"if (this['get'+UTC+'Milliseconds']())\n"
|
||||
"sec += '.' + this['get'+UTC+'Milliseconds']().zeroPad(3)\n"
|
||||
"\n"
|
||||
"var ofs = 'Z';\n"
|
||||
"if (!Date.printAsUTC){\n"
|
||||
"var ofsmin = this.getTimezoneOffset();\n"
|
||||
"if (ofsmin != 0){\n"
|
||||
"ofs = ofsmin > 0 ? '-' : '+'; // This is correct\n"
|
||||
"ofs += (ofsmin/60).zeroPad(2)\n"
|
||||
"ofs += (ofsmin%60).zeroPad(2)\n"
|
||||
"}\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"UTCDate = function(year, month, date, hour, min, sec, ms){\n"
|
||||
"if (!year) return new Date();\n"
|
||||
"\n"
|
||||
"month = (month || 1) - 1; // js is stupid\n"
|
||||
"date = (date || 1);\n"
|
||||
"hour = (hour || 0);\n"
|
||||
"min = (min || 0);\n"
|
||||
"sec = (sec || 0);\n"
|
||||
"ms = (ms || 0);\n"
|
||||
"\n"
|
||||
"// support fractional seconds\n"
|
||||
"if (sec % 1){\n"
|
||||
"ms = Math.round((sec%1) * 1000)\n"
|
||||
"return 'ISODate(\"'+year+'-'+month+'-'+date+'T'+hour+':'+minute+':'+sec+ofs+'\")';\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"return new Date(Date.UTC(year, month, date, hour, min, sec, ms));\n"
|
||||
"Date.printAsUTC = true;\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"ISODate = function(isoDateStr){\n"
|
||||
"if (!isoDateStr)\n"
|
||||
"return new Date();\n"
|
||||
"\n"
|
||||
"var isoDateRegex = /(\\d{4})-?(\\d{2})-?(\\d{2})([T ](\\d{2})(:?(\\d{2})(:?(\\d{2}(\\.\\d+)?))?)?(Z|([+-])(\\d{2}):?(\\d{2})?)?)?/;\n"
|
||||
"var res = isoDateRegex.exec(isoDateStr);\n"
|
||||
"\n"
|
||||
"if (!res)\n"
|
||||
"throw \"invalid ISO date\";\n"
|
||||
"\n"
|
||||
"var year = parseInt(res[1],10) || 1970; // this should always be present\n"
|
||||
"var month = (parseInt(res[2],10) || 1) - 1;\n"
|
||||
"var date = parseInt(res[3],10) || 0;\n"
|
||||
"var hour = parseInt(res[5],10) || 0;\n"
|
||||
"var min = parseInt(res[7],10) || 0;\n"
|
||||
"var sec = parseFloat(res[9]) || 0;\n"
|
||||
"var ms = Math.round((sec%1) * 1000)\n"
|
||||
"sec -= ms/1000\n"
|
||||
"\n"
|
||||
"var time = Date.UTC(year, month, date, hour, min, sec, ms);\n"
|
||||
"\n"
|
||||
"if (res[11] && res[11] != 'Z'){\n"
|
||||
"var ofs = 0;\n"
|
||||
"ofs += (parseInt(res[13],10) || 0) * 60*60*1000; // hours\n"
|
||||
"ofs += (parseInt(res[14],10) || 0) * 60*1000; // mins\n"
|
||||
"if (res[12] == '+') // if ahead subtract\n"
|
||||
"ofs *= -1;\n"
|
||||
"\n"
|
||||
"time += ofs\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"return new Date(time);\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"RegExp.prototype.tojson = RegExp.prototype.toString;\n"
|
||||
"\n"
|
||||
|
||||
Reference in New Issue
Block a user