From 90a8ac9c71dac42197134ca2ae9d8838723c025b Mon Sep 17 00:00:00 2001 From: Bart van den Eijnden Date: Tue, 17 Apr 2012 14:23:56 +0200 Subject: [PATCH 1/3] allow for negative years in dates --- lib/OpenLayers/BaseTypes/Date.js | 2 +- tests/BaseTypes/Date.html | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/OpenLayers/BaseTypes/Date.js b/lib/OpenLayers/BaseTypes/Date.js index f42b713e12..7e721fed0c 100644 --- a/lib/OpenLayers/BaseTypes/Date.js +++ b/lib/OpenLayers/BaseTypes/Date.js @@ -91,7 +91,7 @@ OpenLayers.Date = { */ parse: function(str) { var date; - var match = str.match(/^(?:(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?)?(?:(?:T(\d{1,2}):(\d{2}):(\d{2}(?:\.\d+)?)(Z|(?:[+-]\d{1,2}(?::(\d{2}))?)))|Z)?$/); + var match = str.match(/^(?:(-?\d{4})(?:-(\d{2})(?:-(\d{2}))?)?)?(?:(?:T(\d{1,2}):(\d{2}):(\d{2}(?:\.\d+)?)(Z|(?:[+-]\d{1,2}(?::(\d{2}))?)))|Z)?$/); if (match && (match[1] || match[7])) { // must have at least year or time var year = parseInt(match[1], 10) || 0; var month = (parseInt(match[2], 10) - 1) || 0; diff --git a/tests/BaseTypes/Date.html b/tests/BaseTypes/Date.html index a433d84f24..af12348772 100644 --- a/tests/BaseTypes/Date.html +++ b/tests/BaseTypes/Date.html @@ -35,7 +35,7 @@ function test_Date_parse(t) { - t.plan(114); + t.plan(121); var cases = { "2000": { @@ -139,6 +139,11 @@ year: 2000, month: 3, date: 15 + }, + "-0501-03-01T00:00:00.000Z": { + year: -501, + month: 2, + date: 1 } }; From f07660e8c503a9782fb028154c8faf374cc99bc2 Mon Sep 17 00:00:00 2001 From: Bart van den Eijnden Date: Tue, 17 Apr 2012 15:57:51 +0200 Subject: [PATCH 2/3] implement @ahocevar's suggestion instead --- lib/OpenLayers/BaseTypes/Date.js | 9 ++++++++- tests/BaseTypes/Date.html | 16 ++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/OpenLayers/BaseTypes/Date.js b/lib/OpenLayers/BaseTypes/Date.js index 7e721fed0c..5dba9da4b6 100644 --- a/lib/OpenLayers/BaseTypes/Date.js +++ b/lib/OpenLayers/BaseTypes/Date.js @@ -15,6 +15,13 @@ */ OpenLayers.Date = { + /** + * APIProperty: regex + * The regex to be used for validating dates. You can provide your own + * regex for instance for adding support for years before BC. + */ + regex: /^(?:(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?)?(?:(?:T(\d{1,2}):(\d{2}):(\d{2}(?:\.\d+)?)(Z|(?:[+-]\d{1,2}(?::(\d{2}))?)))|Z)?$/, + /** * APIMethod: toISOString * Generates a string representing a date. The format of the string follows @@ -91,7 +98,7 @@ OpenLayers.Date = { */ parse: function(str) { var date; - var match = str.match(/^(?:(-?\d{4})(?:-(\d{2})(?:-(\d{2}))?)?)?(?:(?:T(\d{1,2}):(\d{2}):(\d{2}(?:\.\d+)?)(Z|(?:[+-]\d{1,2}(?::(\d{2}))?)))|Z)?$/); + var match = str.match(this.regex); if (match && (match[1] || match[7])) { // must have at least year or time var year = parseInt(match[1], 10) || 0; var month = (parseInt(match[2], 10) - 1) || 0; diff --git a/tests/BaseTypes/Date.html b/tests/BaseTypes/Date.html index af12348772..387fcf60f7 100644 --- a/tests/BaseTypes/Date.html +++ b/tests/BaseTypes/Date.html @@ -35,7 +35,7 @@ function test_Date_parse(t) { - t.plan(121); + t.plan(114); var cases = { "2000": { @@ -139,11 +139,6 @@ year: 2000, month: 3, date: 15 - }, - "-0501-03-01T00:00:00.000Z": { - year: -501, - month: 2, - date: 1 } }; @@ -180,6 +175,15 @@ t.ok(isNaN(invalid.getTime()), "invalid has no time"); } + function test_regex(t) { + t.plan(1); + var regex = OpenLayers.Date.regex; + OpenLayers.Date.regex = /^(?:(-?\d{4})(?:-(\d{2})(?:-(\d{2}))?)?)?(?:(?:T(\d{1,2}):(\d{2}):(\d{2}(?:\.\d+)?)(Z|(?:[+-]\d{1,2}(?::(\d{2}))?)))|Z)?$/; + var date = OpenLayers.Date.parse("-0501-03-01T00:00:00.000Z"); + t.ok(!isNaN(date.getTime()), "date with negative year is parsed when providing alternative regex"); + OpenLayers.Date.regex = regex; + } + From a96d32d284d9cfb9bf28d96b04a212bb8b54a322 Mon Sep 17 00:00:00 2001 From: Bart van den Eijnden Date: Tue, 17 Apr 2012 17:39:37 +0200 Subject: [PATCH 3/3] rename to dateRegEx and provide default value in api docs --- lib/OpenLayers/BaseTypes/Date.js | 9 +++++---- tests/BaseTypes/Date.html | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/OpenLayers/BaseTypes/Date.js b/lib/OpenLayers/BaseTypes/Date.js index 5dba9da4b6..937b12d108 100644 --- a/lib/OpenLayers/BaseTypes/Date.js +++ b/lib/OpenLayers/BaseTypes/Date.js @@ -16,11 +16,12 @@ OpenLayers.Date = { /** - * APIProperty: regex + * APIProperty: dateRegEx * The regex to be used for validating dates. You can provide your own - * regex for instance for adding support for years before BC. + * regex for instance for adding support for years before BC. Default + * value is: /^(?:(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?)?(?:(?:T(\d{1,2}):(\d{2}):(\d{2}(?:\.\d+)?)(Z|(?:[+-]\d{1,2}(?::(\d{2}))?)))|Z)?$/ */ - regex: /^(?:(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?)?(?:(?:T(\d{1,2}):(\d{2}):(\d{2}(?:\.\d+)?)(Z|(?:[+-]\d{1,2}(?::(\d{2}))?)))|Z)?$/, + dateRegEx: /^(?:(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?)?(?:(?:T(\d{1,2}):(\d{2}):(\d{2}(?:\.\d+)?)(Z|(?:[+-]\d{1,2}(?::(\d{2}))?)))|Z)?$/, /** * APIMethod: toISOString @@ -98,7 +99,7 @@ OpenLayers.Date = { */ parse: function(str) { var date; - var match = str.match(this.regex); + var match = str.match(this.dateRegEx); if (match && (match[1] || match[7])) { // must have at least year or time var year = parseInt(match[1], 10) || 0; var month = (parseInt(match[2], 10) - 1) || 0; diff --git a/tests/BaseTypes/Date.html b/tests/BaseTypes/Date.html index 387fcf60f7..e54fb31bc3 100644 --- a/tests/BaseTypes/Date.html +++ b/tests/BaseTypes/Date.html @@ -177,11 +177,11 @@ function test_regex(t) { t.plan(1); - var regex = OpenLayers.Date.regex; - OpenLayers.Date.regex = /^(?:(-?\d{4})(?:-(\d{2})(?:-(\d{2}))?)?)?(?:(?:T(\d{1,2}):(\d{2}):(\d{2}(?:\.\d+)?)(Z|(?:[+-]\d{1,2}(?::(\d{2}))?)))|Z)?$/; + var regex = OpenLayers.Date.dateRegEx; + OpenLayers.Date.dateRegEx = /^(?:(-?\d{4})(?:-(\d{2})(?:-(\d{2}))?)?)?(?:(?:T(\d{1,2}):(\d{2}):(\d{2}(?:\.\d+)?)(Z|(?:[+-]\d{1,2}(?::(\d{2}))?)))|Z)?$/; var date = OpenLayers.Date.parse("-0501-03-01T00:00:00.000Z"); t.ok(!isNaN(date.getTime()), "date with negative year is parsed when providing alternative regex"); - OpenLayers.Date.regex = regex; + OpenLayers.Date.dateRegEx = regex; }