From 6eebcc5be01b4f0bcc459f739af7cb5ed882cd7a Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Fri, 7 Oct 2011 10:50:56 -0400 Subject: [PATCH] Making date parsing a bit more lenient. RFC 3339 doesn't say what timezone to assume for date strings without time. Some servers append "Z" to dates (e.g. "2001-02-03Z") and some clients accept this (e.g. Chrome). The OpenLayers date parser now accepts dates without times and with "Z" at the end. --- lib/OpenLayers/BaseTypes/Date.js | 2 +- tests/BaseTypes/Date.html | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/OpenLayers/BaseTypes/Date.js b/lib/OpenLayers/BaseTypes/Date.js index 47ab0102ed..52e40c87d4 100644 --- a/lib/OpenLayers/BaseTypes/Date.js +++ b/lib/OpenLayers/BaseTypes/Date.js @@ -87,7 +87,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}))?)))?$/); + 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 2597207d77..a433d84f24 100644 --- a/tests/BaseTypes/Date.html +++ b/tests/BaseTypes/Date.html @@ -35,7 +35,7 @@ function test_Date_parse(t) { - t.plan(93); + t.plan(114); var cases = { "2000": { @@ -127,6 +127,18 @@ minutes: 51, seconds: 25, milliseconds: 123 + }, + "2000Z": { // lenient (Chrome accepts this) + year: 2000 + }, + "2000-02Z": { // lenient (Chrome accepts this) + year: 2000, + month: 1 + }, + "2000-04-15Z": { // lenient (Chrome accepts this) + year: 2000, + month: 3, + date: 15 } };