From bb5c545e79c3db7f8749d8f8ba3fda606a3bde8d Mon Sep 17 00:00:00 2001 From: Mathias Stearn Date: Wed, 18 Aug 2010 20:19:47 -0400 Subject: [PATCH] UTCDate helper and new printing for Date objects SERVER-1299 --- shell/mongo_vstudio.cpp | 28 +++++++++++++++++++++++++++- shell/utils.js | 27 ++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/shell/mongo_vstudio.cpp b/shell/mongo_vstudio.cpp index 4b69cacbc41..0dd48255f99 100644 --- a/shell/mongo_vstudio.cpp +++ b/shell/mongo_vstudio.cpp @@ -265,9 +265,35 @@ const StringData _jscode_raw_utils = "}\n" "\n" "Date.prototype.tojson = function(){\n" -"return \"\\\"\" + this.toString() + \"\\\"\";\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" "}\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" +"}\n" +"\n" +"return new Date(Date.UTC(year, month, date, hour, min, sec, ms));\n" +"}\n" +"\n" +"\n" "RegExp.prototype.tojson = RegExp.prototype.toString;\n" "\n" "Array.contains = function( a , x ){\n" diff --git a/shell/utils.js b/shell/utils.js index e705feb063d..e7b699cd97f 100644 --- a/shell/utils.js +++ b/shell/utils.js @@ -260,7 +260,32 @@ Date.timeFunc = function( theFunc , numTimes ){ } Date.prototype.tojson = function(){ - return "\"" + this.toString() + "\""; + var year = this.getUTCFullYear(); + var month = this.getUTCMonth() + 1; // js is stupid + var date = this.getUTCDate(); + var hour = this.getUTCHours(); + var minute = this.getUTCMinutes(); + var sec = this.getUTCSeconds() + (this.getUTCMilliseconds()/1000); + + return 'UTCDate('+year+', '+month+', '+date+', '+hour+', '+minute+', '+sec+')'; +} + +UTCDate = function(year, month, date, hour, min, sec, ms){ + if (!year) return new Date(); + + month = (month || 1) - 1; // js is stupid + date = (date || 1); + hour = (hour || 0); + min = (min || 0); + sec = (sec || 0); + ms = (ms || 0); + + // support fractional seconds + if (sec % 1){ + ms = Math.round((sec%1) * 1000) + } + + return new Date(Date.UTC(year, month, date, hour, min, sec, ms)); } RegExp.prototype.tojson = RegExp.prototype.toString;