Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixed ISO parse bug

  • Loading branch information...
commit 242ce8cd2fe4781467ff8f9eb2e422119073e23d 1 parent f03db4c
@coolaj86 authored
Showing with 69 additions and 7 deletions.
  1. +44 −0 date-test2.js
  2. +25 −7 date.js
View
44 date-test2.js
@@ -0,0 +1,44 @@
+(function () {
+ "use strict";
+
+ require('./date');
+
+ var assert = require('assert')
+ , pairs = [
+ // 0 + epoch +- 1
+ ["epoch_sec", "1970-01-01T00:00:00Z", "1970-01-01T00:00:00+00:00"],
+ ["epoch_min0", "1970-01-01T00:00Z", "1970-01-01T00:00:00+00:00"],
+ ["epoch_min1", "1970-01-01T00:00:00Z", "1970-01-01T00:00+00:00"],
+ ["epoch_+1m", "1970-01-01T00:00Z", "1970-01-01T00:01+00:01"],
+ ["epoch_-1m", "1970-01-01T00:00Z", "1969-12-31T23:59-00:01"],
+ ["epoch_+1h", "1970-01-01T00:00:00Z", "1970-01-01T01:00:00+01:00"],
+ ["epoch_-1h", "1970-01-01T00:00:00Z", "1969-12-31T23:00:00-01:00"],
+
+ // 1 +- epoch + 0
+ ["1h-epoch", "1969-12-31T23:00Z", "1970-01-01T00:00+01:00"],
+ ["1h+epoch", "1970-01-01T01:00:00Z", "1970-01-01T00:00:00-01:00"],
+ ["1m-epoch", "1969-12-31T23:59:00Z", "1970-01-01T00:00+00:01"],
+ ["1m+epoch", "1970-01-01T00:01Z", "1970-01-01T00:00:00-00:01"],
+
+
+ ["+00:00", "2011-03-30T16:48:15Z", "2011-03-30T16:48:15+00:00"],
+ ["-01:00", "2011-03-30T17:48:15Z", "2011-03-30T16:48:15-01:00"],
+ ["+01:00", "2011-03-30T15:48:15Z", "2011-03-30T16:48:15+01:00"],
+ ["-11:11", "2011-03-30T16:48:15Z", "2011-03-30T05:37:15-11:11"],
+ ["+11:11", "2011-03-30T05:37:15Z", "2011-03-30T16:48:15+11:11"],
+
+ // across dateline
+ ["-11:00", "2011-04-01T03:00:01Z", "2011-03-31T16:00:01-11:00"],
+ ["+11:00", "2011-03-31T18:00:01Z", "2011-04-01T05:00:01+11:00"],
+ ];
+
+ pairs.forEach(function (pair) {
+ var date1 = pair[1]
+ , date2 = pair[2];
+
+ //console.log(pair[0]);
+ assert.equal(Date.ISO(date1).toString(), Date.ISO(date2).toString());
+ //assert.equal(Date.ISO(date1).valueOf(), Date.ISO(date2).valueOf());
+ });
+ console.log("datline tests passed");
+}());
View
32 date.js
@@ -1,7 +1,7 @@
-/*jslint onevar: true, undef: true, newcap: true, nomen: true, regexp: true, plusplus: true, strict: true */
/*global require: true, provide: true */
(function () {
"use strict";
+ /*jslint onevar: true, undef: true, newcap: true, nomen: true, regexp: true, plusplus: true, strict: true */
require('require-kiss');
@@ -12,6 +12,12 @@
* @compatibility Chrome, Firefox, IE 5+, Opera, Safari, WebKit, Others
*/
+ function debug(matches) {
+ matches.forEach(function (match, i) {
+ console.log(i, match);
+ });
+ }
+
function ISO(datestring) {
// 1 3 5 7 8 10 12 13 15 16 17
// yyyy - mm - dd 'T' HH : MM : ss .ms 'Z' +/- HH : MM
@@ -21,14 +27,16 @@
date = new Date(),
hour,
minute,
- opstring;
+ aheadOfUtc;
if(null === match) {
return false;
// throw new Error("Invalid ISO String");
}
- // The bit-shifting is shorthand for `(undefined !== m) && m.valueOf(m) || 0`
+ //debug(match);
+
+ // The bit-shifting is shorthand for `Number(m) || 0`
date.setUTCFullYear(match[1] >> 0);
date.setUTCMonth(match[3] ? (match[3] >> 0) - 1 : 0);
date.setUTCDate(match[5] >> 0);
@@ -36,13 +44,23 @@
date.setUTCMinutes(match[8] >> 0);
date.setUTCSeconds(match[10] >> 0);
date.setUTCMilliseconds(match[12] >> 0);
+
+ // Adjust to UTC offset
if (match[13] && match[13] !== "Z") {
hour = match[16] >> 0;
minute = match[17] >> 0;
- opstring = (match[15] === "+");
+ aheadOfUtc = (match[15] === "+");
+
+ hour = hour * 60 * 60 * 1000;
+ minute = minute * 60 * 1000;
+
+ if (aheadOfUtc) {
+ hour = -hour;
+ minute = -minute;
+ }
- date.setUTCHours((match[7] >> 0) + opstring ? -hour : hour);
- date.setUTCMinutes((match[8] >> 0) + opstring ? -minute : minute);
+ // easy dateline wrapping
+ date = new Date(date.valueOf() + hour + minute);
}
return date;
}
@@ -92,5 +110,5 @@
Date.prototype.toISOString = Date.prototype.toISOString || toISOString;
}
- provide('date');
+ provide('./date');
}());
Please sign in to comment.
Something went wrong with that request. Please try again.