From 769e8bc7b8799385e2677b26a5c0a72e839f44ca Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Thu, 22 Dec 2011 15:38:17 -0500 Subject: [PATCH] Fix issue 237 (standard date/time string) This incorporates a patch from Steve Flenniken to support ISO 8601-format (YYYY-MM-DDTHH:MM:SS.ddddddZ) dates. Also includes a regression test. --- auto_tests/misc/local.html | 1 + auto_tests/tests/date_formats.js | 35 +++++++++++++++++++++++++++++++++++ dygraph-utils.js | 5 +++++ dygraph.js | 1 + 4 files changed, 42 insertions(+) create mode 100644 auto_tests/tests/date_formats.js diff --git a/auto_tests/misc/local.html b/auto_tests/misc/local.html index cb9e2f1..243e513 100644 --- a/auto_tests/misc/local.html +++ b/auto_tests/misc/local.html @@ -34,6 +34,7 @@ + diff --git a/auto_tests/tests/date_formats.js b/auto_tests/tests/date_formats.js new file mode 100644 index 0000000..27ddadb --- /dev/null +++ b/auto_tests/tests/date_formats.js @@ -0,0 +1,35 @@ +/** + * @fileoverview Tests that various formats of date are understood by dygraphs. + * + * @author dan@dygraphs.com (Dan Vanderkam) + */ +var dateFormatsTestCase = TestCase("date-formats"); + +dateFormatsTestCase.prototype.setUp = function() { +}; + +dateFormatsTestCase.prototype.tearDown = function() { +}; + +dateFormatsTestCase.prototype.testISO8601 = function() { + // Format: YYYY-MM-DDTHH:MM:SS.ddddddZ + // The "Z" indicates UTC, so this test should pass regardless of the time + // zone of the machine on which it is run. + assertEquals(946816496789, Dygraph.dateParser("2000-01-02T12:34:56.789012Z")); +}; + +dateFormatsTestCase.prototype.testHyphenatedDate = function() { + // Format: YYYY-MM-DD HH:MM + + // Midnight February 2, 2000, UTC + var d = new Date(Date.UTC(2000, 1, 2)); + + // Convert to a string in the local time zone: YYYY-DD-MM HH:MM + var zp = function(x) { return x < 10 ? '0' + x : x; }; + var str = d.getFullYear() + '-' + + zp(1 + d.getMonth()) + '-' + + zp(d.getDate()) + ' ' + + zp(d.getHours()) + ':' + + zp(d.getMinutes()); + assertEquals(Date.UTC(2000, 1, 2), Dygraph.dateParser(str)); +}; diff --git a/dygraph-utils.js b/dygraph-utils.js index 104d00f..3d77148 100644 --- a/dygraph-utils.js +++ b/dygraph-utils.js @@ -465,6 +465,11 @@ Dygraph.binarySearch = function(val, arry, abs, low, high) { Dygraph.dateParser = function(dateStr) { var dateStrSlashed; var d; + + // Let the system try the format first. + d = Dygraph.dateStrToMillis(dateStr); + if (d && !isNaN(d)) return d; + if (dateStr.search("-") != -1) { // e.g. '2009-7-12' or '2009-07-12' dateStrSlashed = dateStr.replace("-", "/", "g"); while (dateStrSlashed.search("-") != -1) { diff --git a/dygraph.js b/dygraph.js index 2dc4610..a5e1b56 100644 --- a/dygraph.js +++ b/dygraph.js @@ -750,6 +750,7 @@ Dygraph.prototype.numRows = function() { /** * Returns the full range of the x-axis, as determined by the most extreme * values in the data set. Not affected by zooming, visibility, etc. + * TODO(danvk): merge w/ xAxisExtremes * @return { Array } A [low, high] pair * @private */ -- 2.7.4