Repository: jsonata-js/jsonata (js)

Issue:
ISO 8601 check for toMillis is too strict
The ISO 8601 date check for the `toMillis()` function is too strict and only accepts dates in the format `2018-01-31T17:39:00.000Z` or `2018-01-31T17:39:00.000+00:00` rather than the wider range of valid dates which is possible such as `2018-01-31` or `2018-01-31T17:39:00.000+0000` (the latter of which is where I stumbled upon this issue).

Pull request description:
Update regex in toMillis to allow more complete range of ISO 8601 compatible dates
Fixes #140 by adjusting the regex to a complete range of ISO 8601 dates which `Date.parse()` is subsequently happy with. Also updated one of the `toMillis()` test cases to use a date format which is now compatible through this.

Patch:
diff --git a/jsonata.js b/jsonata.js
index 4445696..a0e9255 100644
--- a/jsonata.js
+++ b/jsonata.js
@@ -4653,7 +4653,8 @@ var jsonata = (function() {
     }
 
     // Regular expression to match an ISO 8601 formatted timestamp
-    var iso8601regex = new RegExp('^\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\d\\.\\d+([+-][0-2]\\d:[0-5]\\d|Z)$');
+    // Uses https://www.myintervals.com/blog/2009/05/20/iso-8601-date-validation-that-doesnt-suck/ but removes the "week number" format not supported by Date.parse
+    var iso8601regex = new RegExp('^\\d{4}(-[01]\\d)*(-[0-3]\\d)*(T[0-2]\\d:[0-5]\\d:[0-5]\\d)*(\\.\\d+)?([+-][0-2]\\d:?[0-5]\\d|Z)?$');
 
     /**
      * Converts an ISO 8601 timestamp to milliseconds since the epoch


Test patch:
diff --git a/test/test-suite/groups/function-sift/case004.json b/test/test-suite/groups/function-sift/case004.json
deleted file mode 100644
index 9e4617d..0000000
--- a/test/test-suite/groups/function-sift/case004.json
+++ /dev/null
@@ -1,6 +0,0 @@
-{
-    "expr": "$toMillis(\"2017-10-30T16:25:32.935Z\")",
-    "dataset": "dataset5",
-    "bindings": {},
-    "result": 1509380732935
-}
\ No newline at end of file
diff --git a/test/test-suite/groups/function-sift/case003.json b/test/test-suite/groups/function-tomillis/case001.json
similarity index 100%
rename from test/test-suite/groups/function-sift/case003.json
rename to test/test-suite/groups/function-tomillis/case001.json
diff --git a/test/test-suite/groups/function-tomillis/case002.json b/test/test-suite/groups/function-tomillis/case002.json
new file mode 100644
index 0000000..9dff9e6
--- /dev/null
+++ b/test/test-suite/groups/function-tomillis/case002.json
@@ -0,0 +1,6 @@
+{
+    "expr": "$toMillis(\"2018-02-01T09:42:13.123+0000\")",
+    "dataset": "dataset5",
+    "bindings": {},
+    "result": 1517478133123
+}
\ No newline at end of file
diff --git a/test/test-suite/groups/function-tomillis/case003.json b/test/test-suite/groups/function-tomillis/case003.json
new file mode 100644
index 0000000..f026226
--- /dev/null
+++ b/test/test-suite/groups/function-tomillis/case003.json
@@ -0,0 +1,6 @@
+{
+    "expr": "$toMillis(\"2017-10-30\")",
+    "dataset": "dataset5",
+    "bindings": {},
+    "result": 1509321600000
+}
\ No newline at end of file
diff --git a/test/test-suite/groups/function-tomillis/case004.json b/test/test-suite/groups/function-tomillis/case004.json
new file mode 100644
index 0000000..f05ddea
--- /dev/null
+++ b/test/test-suite/groups/function-tomillis/case004.json
@@ -0,0 +1,6 @@
+{
+    "expr": "$toMillis(\"2017-10-30T15:28:34\")",
+    "dataset": "dataset5",
+    "bindings": {},
+    "result": 1509377314000
+}
\ No newline at end of file
diff --git a/test/test-suite/groups/function-tomillis/case005.json b/test/test-suite/groups/function-tomillis/case005.json
new file mode 100644
index 0000000..e85bdd4
--- /dev/null
+++ b/test/test-suite/groups/function-tomillis/case005.json
@@ -0,0 +1,6 @@
+{
+    "expr": "$toMillis(\"2018\")",
+    "dataset": "dataset5",
+    "bindings": {},
+    "result": 1514764800000
+}
\ No newline at end of file
diff --git a/test/test-suite/groups/function-sift/case005.json b/test/test-suite/groups/function-tomillis/case006.json
similarity index 100%
rename from test/test-suite/groups/function-sift/case005.json
rename to test/test-suite/groups/function-tomillis/case006.json
diff --git a/test/test-suite/groups/function-sift/case006.json b/test/test-suite/groups/function-tomillis/case007.json
similarity index 100%
rename from test/test-suite/groups/function-sift/case006.json
rename to test/test-suite/groups/function-tomillis/case007.json
diff --git a/test/test-suite/groups/function-tomillis/case008.json b/test/test-suite/groups/function-tomillis/case008.json
new file mode 100644
index 0000000..e546635
--- /dev/null
+++ b/test/test-suite/groups/function-tomillis/case008.json
@@ -0,0 +1,6 @@
+{
+    "expr": "$toMillis(\"01-02-2018\")",
+    "dataset": "dataset5",
+    "bindings": {},
+    "code": "D3110"
+}
\ No newline at end of file
diff --git a/test/test-suite/groups/function-tomillis/case009.json b/test/test-suite/groups/function-tomillis/case009.json
new file mode 100644
index 0000000..1a0bc24
--- /dev/null
+++ b/test/test-suite/groups/function-tomillis/case009.json
@@ -0,0 +1,6 @@
+{
+    "expr": "$toMillis(\"2018-02-03 11:15:33\")",
+    "dataset": "dataset5",
+    "bindings": {},
+    "code": "D3110"
+}
\ No newline at end of file