Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #413 from bartvde/extendeddate

allow the regex for date parsing to be specified, so that applications can potentially deal with dates before BC in ISO-8601 (r=@ahocevar)
  • Loading branch information...
commit ce189946640cac27152bfb928ef55b9a41313e09 2 parents b683c9b + a96d32d
Bart van den Eijnden authored
Showing with 18 additions and 1 deletion.
  1. +9 −1 lib/OpenLayers/BaseTypes/Date.js
  2. +9 −0 tests/BaseTypes/Date.html
10 lib/OpenLayers/BaseTypes/Date.js
View
@@ -15,6 +15,14 @@
*/
OpenLayers.Date = {
+ /**
+ * 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. 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)?$/
+ */
+ 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
* Generates a string representing a date. The format of the string follows
@@ -91,7 +99,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.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;
9 tests/BaseTypes/Date.html
View
@@ -175,6 +175,15 @@
t.ok(isNaN(invalid.getTime()), "invalid has no time");
}
+ function test_regex(t) {
+ t.plan(1);
+ 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.dateRegEx = regex;
+ }
+
</script>
</head>
<body>
Please sign in to comment.
Something went wrong with that request. Please try again.