changeset 503:1d29d2e27590

8019985: Date.parse("2000-01-01T00:00:00.Z") should return NaN Reviewed-by: sundar, jlaskey
author hannesw
date Fri, 16 Aug 2013 13:42:44 +0200
parents 09c99b58b81e
children 36fb36217e1d
files src/jdk/nashorn/internal/parser/DateParser.java test/script/basic/JDK-8019985.js
diffstat 2 files changed, 71 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk/nashorn/internal/parser/DateParser.java	Fri Aug 16 15:04:36 2013 +0530
+++ b/src/jdk/nashorn/internal/parser/DateParser.java	Fri Aug 16 13:42:44 2013 +0200
@@ -141,7 +141,7 @@
      * Try parsing the date string according to the rules laid out in ES5 15.9.1.15.
      * The date string must conform to the following format:
      *
-     * <pre>  [('-'|'+')yy]yyyy[-MM[-dd]][hh:mm[:ss[.sss]][Z|(+|-)hh:mm]] </pre>
+     * <pre>  [('-'|'+')yy]yyyy[-MM[-dd]][Thh:mm[:ss[.sss]][Z|(+|-)hh:mm]] </pre>
      *
      * <p>If the string does not contain a time zone offset, the <tt>TIMEZONE</tt> field
      * is set to <tt>0</tt> (GMT).</p>
@@ -249,7 +249,7 @@
 
             switch (token) {
                 case NUMBER:
-                    if (skip(':')) {
+                    if (skipDelimiter(':')) {
                         // A number followed by ':' is parsed as time
                         if (!setTimeField(numValue)) {
                             return false;
@@ -260,14 +260,14 @@
                             if (token != Token.NUMBER || !setTimeField(numValue)) {
                                 return false;
                             }
-                        } while (skip(isSet(SECOND) ? '.' : ':'));
+                        } while (skipDelimiter(isSet(SECOND) ? '.' : ':'));
 
                     } else {
                         // Parse as date token
                         if (!setDateField(numValue)) {
                             return false;
                         }
-                        skip('-');
+                        skipDelimiter('-');
                     }
                     break;
 
@@ -297,7 +297,7 @@
                             break;
                     }
                     if (nameValue.type != Name.TIMEZONE_ID) {
-                        skip('-');
+                        skipDelimiter('-');
                     }
                     break;
 
@@ -359,7 +359,18 @@
         return pos < length ? string.charAt(pos) : -1;
     }
 
-    private boolean skip(final char c) {
+    // Skip delimiter if followed by a number. Used for ISO 8601 formatted dates
+    private boolean skipNumberDelimiter(final char c) {
+        if (pos < length - 1 && string.charAt(pos) == c
+                && Character.getType(string.charAt(pos + 1)) == DECIMAL_DIGIT_NUMBER) {
+            token = null;
+            pos++;
+            return true;
+        }
+        return false;
+    }
+
+    private boolean skipDelimiter(final char c) {
         if (pos < length && string.charAt(pos) == c) {
             token = null;
             pos++;
@@ -452,14 +463,14 @@
         switch (currentField) {
             case YEAR:
             case MONTH:
-                return skip('-') || peek() == 'T' || peek() == -1;
+                return skipNumberDelimiter('-') || peek() == 'T' || peek() == -1;
             case DAY:
                 return peek() == 'T' || peek() == -1;
             case HOUR:
             case MINUTE:
-                return skip(':') || endOfTime();
+                return skipNumberDelimiter(':') || endOfTime();
             case SECOND:
-                return skip('.') || endOfTime();
+                return skipNumberDelimiter('.') || endOfTime();
             default:
                 return true;
         }
@@ -515,7 +526,7 @@
     private int readTimeZoneOffset() {
         final int sign = string.charAt(pos - 1) == '+' ? 1 : -1;
         int offset = readNumber(2);
-        skip(':');
+        skipDelimiter(':');
         offset = offset * 60 + readNumber(2);
         return sign * offset;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/script/basic/JDK-8019985.js	Fri Aug 16 13:42:44 2013 +0200
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8019985: Date.parse("2000-01-01T00:00:00.Z") should return NaN
+ *
+ * @test
+ * @run
+ */
+
+function testFail(str) {
+    if (!isNaN(Date.parse(str))) {
+        throw new Error("Parsed invalid date string: " + str);
+    }
+}
+
+function testOk(str) {
+    if (isNaN(Date.parse(str))) {
+        throw new Error("Failed to parse valid date string: " + str);
+    }
+}
+
+testFail("2000-01-01T00:00:00.Z");
+testFail("2000-01-01T00:00:Z");
+testFail("2000-01-01T00:Z");
+testFail("2000-01-01T00Z");
+testOk("2000-01-01T00:00:00.000Z");
+testOk("2000-01-01T00:00:00.0Z");
+testOk("2000-01-01T00:00:00Z");
+testOk("2000-01-01T00:00Z");