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:
@@ -632,9 +632,11 @@ OpenLayers.Date = {
|
||||
* APIMethod: parse
|
||||
* 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
|
||||
* http://tools.ietf.org/html/rfc3339). If the parse method on
|
||||
* the Date constructor returns a valid date for the given string,
|
||||
* that method is used.
|
||||
* http://tools.ietf.org/html/rfc3339). We don't call the native
|
||||
* Date.parse because of inconsistency between implmentations. In
|
||||
* 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:
|
||||
* str - {String} A string representing the date (e.g.
|
||||
@@ -647,37 +649,31 @@ OpenLayers.Date = {
|
||||
*/
|
||||
parse: function(str) {
|
||||
var date;
|
||||
// first check if the native parse method can parse it
|
||||
var elapsed = Date.parse(str);
|
||||
if (!isNaN(elapsed)) {
|
||||
date = new Date(elapsed);
|
||||
} else {
|
||||
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}))?)))?$/);
|
||||
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;
|
||||
var day = parseInt(match[3], 10) || 1;
|
||||
date = new Date(Date.UTC(year, month, day));
|
||||
// optional time
|
||||
var type = match[7];
|
||||
if (type) {
|
||||
var hours = parseInt(match[4], 10);
|
||||
var minutes = parseInt(match[5], 10);
|
||||
var secFrac = parseFloat(match[6]);
|
||||
var seconds = secFrac | 0;
|
||||
var milliseconds = Math.round(1000 * (secFrac - seconds));
|
||||
date.setUTCHours(hours, minutes, seconds, milliseconds);
|
||||
// check 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);
|
||||
}
|
||||
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}))?)))?$/);
|
||||
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;
|
||||
var day = parseInt(match[3], 10) || 1;
|
||||
date = new Date(Date.UTC(year, month, day));
|
||||
// optional time
|
||||
var type = match[7];
|
||||
if (type) {
|
||||
var hours = parseInt(match[4], 10);
|
||||
var minutes = parseInt(match[5], 10);
|
||||
var secFrac = parseFloat(match[6]);
|
||||
var seconds = secFrac | 0;
|
||||
var milliseconds = Math.round(1000 * (secFrac - seconds));
|
||||
date.setUTCHours(hours, minutes, seconds, milliseconds);
|
||||
// check 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user