diff options
author | Dan Pasette <dan@10gen.com> | 2013-07-01 10:42:36 -0400 |
---|---|---|
committer | Dan Pasette <dan@10gen.com> | 2013-07-01 10:42:36 -0400 |
commit | 5540865628d166f88da833d7470fd3f792c8017d (patch) | |
tree | b2ab542a32c9e24c3b6d7a0e3eff7ce794a16917 | |
parent | ad58211471756440cf9c36bc3d9598b50520f9d0 (diff) | |
download | mongo-5540865628d166f88da833d7470fd3f792c8017d.tar.gz |
SERVER-7588 Improve logic for seconds and milliseconds in ISODate constructor
Do not use floating point for seconds, only for milliseconds. Adjust other
date components when milliseconds round up to 1000.
Code backported from Tad Marshall's master branch commit:
https://github.com/mongodb/mongo/commit/7cf042ff3e0c97a7fe701cd85f5cdd4c1519179b
-rw-r--r-- | src/mongo/shell/utils.js | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/src/mongo/shell/utils.js b/src/mongo/shell/utils.js index 4497400ee72..41304d1055f 100644 --- a/src/mongo/shell/utils.js +++ b/src/mongo/shell/utils.js @@ -412,9 +412,29 @@ ISODate = function(isoDateStr){ var date = parseInt(res[3],10) || 0; var hour = parseInt(res[5],10) || 0; var min = parseInt(res[7],10) || 0; - var sec = parseFloat(res[9]) || 0; - var ms = Math.round((sec%1) * 1000) - sec -= ms/1000 + var sec = parseInt((res[9] && res[9].substr(0,2)),10) || 0; + var ms = Math.round((parseFloat(res[10]) || 0) * 1000); + if (ms == 1000) { + ms = 0; + ++sec; + } + if (sec == 60) { + sec = 0; + ++min; + } + if (min == 60) { + min = 0; + ++hour; + } + if (hour == 24) { + hour = 0; // the day wrapped, let JavaScript figure out the rest + var tempTime = Date.UTC(year, month, date, hour, min, sec, ms); + tempTime += 24 * 60 * 60 * 1000; // milliseconds in a day + var tempDate = new Date(tempTime); + year = tempDate.getUTCFullYear(); + month = tempDate.getUTCMonth(); + date = tempDate.getUTCDate(); + } var time = Date.UTC(year, month, date, hour, min, sec, ms); |