Getting consistent datestring parsing in Chrome and Firefox. r=crschmidt (closes #2994)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@11281 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2011-02-23 09:32:36 +00:00
parent abb2bb509d
commit 75538b6f42
+28 -32
View File
@@ -632,9 +632,11 @@ OpenLayers.Date = {
* APIMethod: parse * APIMethod: parse
* Generate a date object from a string. The format for the string follows * Generate a date object from a string. The format for the string follows
* the profile of ISO 8601 for date and time on the Internet (see * the profile of ISO 8601 for date and time on the Internet (see
* http://tools.ietf.org/html/rfc3339). If the parse method on * http://tools.ietf.org/html/rfc3339). We don't call the native
* the Date constructor returns a valid date for the given string, * Date.parse because of inconsistency between implmentations. In
* that method is used. * Chrome, calling Date.parse with a string that doesn't contain any
* indication of the timezone (e.g. "2011"), the date is interpreted
* in local time. On Firefox, the assumption is UTC.
* *
* Parameters: * Parameters:
* str - {String} A string representing the date (e.g. * str - {String} A string representing the date (e.g.
@@ -647,37 +649,31 @@ OpenLayers.Date = {
*/ */
parse: function(str) { parse: function(str) {
var date; var date;
// first check if the native parse method can parse it 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 elapsed = Date.parse(str); if (match && (match[1] || match[7])) { // must have at least year or time
if (!isNaN(elapsed)) { var year = parseInt(match[1], 10) || 0;
date = new Date(elapsed); var month = (parseInt(match[2], 10) - 1) || 0;
} else { var day = parseInt(match[3], 10) || 1;
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}))?)))?$/); date = new Date(Date.UTC(year, month, day));
if (match && (match[1] || match[7])) { // must have at least year or time // optional time
var year = parseInt(match[1], 10) || 0; var type = match[7];
var month = (parseInt(match[2], 10) - 1) || 0; if (type) {
var day = parseInt(match[3], 10) || 1; var hours = parseInt(match[4], 10);
date = new Date(Date.UTC(year, month, day)); var minutes = parseInt(match[5], 10);
// optional time var secFrac = parseFloat(match[6]);
var type = match[7]; var seconds = secFrac | 0;
if (type) { var milliseconds = Math.round(1000 * (secFrac - seconds));
var hours = parseInt(match[4], 10); date.setUTCHours(hours, minutes, seconds, milliseconds);
var minutes = parseInt(match[5], 10); // check offset
var secFrac = parseFloat(match[6]); if (type !== "Z") {
var seconds = secFrac | 0; var hoursOffset = parseInt(type, 10);
var milliseconds = Math.round(1000 * (secFrac - seconds)); var minutesOffset = parseInt(match[8], 10) || 0;
date.setUTCHours(hours, minutes, seconds, milliseconds); var offset = -1000 * (60 * (hoursOffset * 60) + minutesOffset * 60);
// check offset date = new Date(date.getTime() + offset);
if (type !== "Z") {
var hoursOffset = parseInt(type, 10);
var minutesOffset = parseInt(match[8], 10) || 0;
var offset = -1000 * (60 * (hoursOffset * 60) + minutesOffset * 60);
date = new Date(date.getTime() + offset);
}
} }
} else {
date = new Date("invalid");
} }
} else {
date = new Date("invalid");
} }
return date; return date;
} }