implement @ahocevar's suggestion instead

This commit is contained in:
Bart van den Eijnden
2012-04-17 15:57:51 +02:00
parent 90a8ac9c71
commit f07660e8c5
2 changed files with 18 additions and 7 deletions

View File

@@ -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;

View File

@@ -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;
}
</script>
</head>
<body>