changeset 582:b8d9a63578e2

8025163: Date methods should not return -0 Reviewed-by: lagergren, jlaskey
author hannesw
date Sat, 21 Sep 2013 10:11:15 +0200
parents 16b6db9f7225
children 8f6304373671 c5475f5d4647
files src/jdk/nashorn/internal/objects/NativeDate.java test/script/basic/JDK-8025163.js test/script/basic/JDK-8025163.js.EXPECTED
diffstat 3 files changed, 110 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk/nashorn/internal/objects/NativeDate.java	Fri Sep 20 22:37:08 2013 +0530
+++ b/src/jdk/nashorn/internal/objects/NativeDate.java	Sat Sep 21 10:11:15 2013 +0200
@@ -75,11 +75,11 @@
     private static final int FORMAT_LOCAL_TIME      = 5;
 
     // Constants defined in ECMA 15.9.1.10
-    private static final double hoursPerDay      = 24;
-    private static final double minutesPerHour   = 60;
-    private static final double secondsPerMinute = 60;
-    private static final double msPerSecond   = 1_000;
-    private static final double msPerMinute  = 60_000;
+    private static final int    hoursPerDay      = 24;
+    private static final int    minutesPerHour   = 60;
+    private static final int    secondsPerMinute = 60;
+    private static final int    msPerSecond   = 1_000;
+    private static final int    msPerMinute  = 60_000;
     private static final double msPerHour = 3_600_000;
     private static final double msPerDay = 86_400_000;
 
@@ -926,13 +926,13 @@
                 case FORMAT_DATE :
                 case FORMAT_LOCAL_DATE_TIME:
                     // EEE MMM dd yyyy
-                    sb.append(weekDays[(int) weekDay(t)])
+                    sb.append(weekDays[weekDay(t)])
                             .append(' ')
-                            .append(months[(int) monthFromTime(t)])
+                            .append(months[monthFromTime(t)])
                             .append(' ');
-                    zeroPad(sb, (int) dayFromTime(t), 2);
+                    zeroPad(sb, dayFromTime(t), 2);
                     sb.append(' ');
-                    zeroPad(sb, (int) yearFromTime(t), 4);
+                    zeroPad(sb, yearFromTime(t), 4);
                     if (format == FORMAT_DATE) {
                         break;
                     }
@@ -948,11 +948,11 @@
                     offset = (offset / 60) * 100 + offset % 60;
 
                     // HH:mm:ss GMT+HHmm
-                    zeroPad(sb, (int) hourFromTime(t), 2);
+                    zeroPad(sb, hourFromTime(t), 2);
                     sb.append(':');
-                    zeroPad(sb, (int) minFromTime(t), 2);
+                    zeroPad(sb, minFromTime(t), 2);
                     sb.append(':');
-                    zeroPad(sb, (int) secFromTime(t), 2);
+                    zeroPad(sb, secFromTime(t), 2);
                     sb.append(" GMT")
                             .append(offset < 0 ? '-' : '+');
                     zeroPad(sb, Math.abs(offset), 4);
@@ -963,20 +963,20 @@
 
                 case FORMAT_LOCAL_DATE:
                     // yyyy-MM-dd
-                    zeroPad(sb, (int) yearFromTime(t), 4);
+                    zeroPad(sb, yearFromTime(t), 4);
                     sb.append('-');
-                    zeroPad(sb, (int) monthFromTime(t) + 1, 2);
+                    zeroPad(sb, monthFromTime(t) + 1, 2);
                     sb.append('-');
-                    zeroPad(sb, (int) dayFromTime(t), 2);
+                    zeroPad(sb, dayFromTime(t), 2);
                     break;
 
                 case FORMAT_LOCAL_TIME:
                     // HH:mm:ss
-                    zeroPad(sb, (int) hourFromTime(t), 2);
+                    zeroPad(sb, hourFromTime(t), 2);
                     sb.append(':');
-                    zeroPad(sb, (int) minFromTime(t), 2);
+                    zeroPad(sb, minFromTime(t), 2);
                     sb.append(':');
-                    zeroPad(sb, (int) secFromTime(t), 2);
+                    zeroPad(sb, secFromTime(t), 2);
                     break;
 
                 default:
@@ -996,19 +996,19 @@
             final StringBuilder sb = new StringBuilder(29);
             final double t = nd.getTime();
             // EEE, dd MMM yyyy HH:mm:ss z
-            sb.append(weekDays[(int) weekDay(t)])
+            sb.append(weekDays[weekDay(t)])
                     .append(", ");
-            zeroPad(sb, (int) dayFromTime(t), 2);
+            zeroPad(sb, dayFromTime(t), 2);
             sb.append(' ')
-                    .append(months[(int) monthFromTime(t)])
+                    .append(months[monthFromTime(t)])
                     .append(' ');
-            zeroPad(sb, (int) yearFromTime(t), 4);
+            zeroPad(sb, yearFromTime(t), 4);
             sb.append(' ');
-            zeroPad(sb, (int) hourFromTime(t), 2);
+            zeroPad(sb, hourFromTime(t), 2);
             sb.append(':');
-            zeroPad(sb, (int) minFromTime(t), 2);
+            zeroPad(sb, minFromTime(t), 2);
             sb.append(':');
-            zeroPad(sb, (int) secFromTime(t), 2);
+            zeroPad(sb, secFromTime(t), 2);
             sb.append(" GMT");
             return sb.toString();
         }
@@ -1023,19 +1023,19 @@
             final StringBuilder sb = new StringBuilder(24);
             final double t = nd.getTime();
             // yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
-            zeroPad(sb, (int) yearFromTime(t), 4);
+            zeroPad(sb, yearFromTime(t), 4);
             sb.append('-');
-            zeroPad(sb, (int) monthFromTime(t) + 1, 2);
+            zeroPad(sb, monthFromTime(t) + 1, 2);
             sb.append('-');
-            zeroPad(sb, (int) dayFromTime(t), 2);
+            zeroPad(sb, dayFromTime(t), 2);
             sb.append('T');
-            zeroPad(sb, (int) hourFromTime(t), 2);
+            zeroPad(sb, hourFromTime(t), 2);
             sb.append(':');
-            zeroPad(sb, (int) minFromTime(t), 2);
+            zeroPad(sb, minFromTime(t), 2);
             sb.append(':');
-            zeroPad(sb, (int) secFromTime(t), 2);
+            zeroPad(sb, secFromTime(t), 2);
             sb.append('.');
-            zeroPad(sb, (int) msFromTime(t), 3);
+            zeroPad(sb, msFromTime(t), 3);
             sb.append("Z");
             return sb.toString();
         }
@@ -1072,29 +1072,30 @@
     }
 
     // ECMA 15.9.1.3 Year Number
-    private static double timeFromYear(final double y) {
+    private static double timeFromYear(final int y) {
         return dayFromYear(y) * msPerDay;
     }
 
-    private static double yearFromTime(final double t) {
-        double y = Math.floor(t / (msPerDay * 365.2425)) + 1970;
+    // ECMA 15.9.1.3 Year Number
+    private static int yearFromTime(final double t) {
+        int y = (int) Math.floor(t / (msPerDay * 365.2425)) + 1970;
         final double t2 = timeFromYear(y);
         if (t2 > t) {
             y--;
-        } else if (t2 + msPerDay * daysInYear((int) y) <= t) {
+        } else if (t2 + msPerDay * daysInYear(y) <= t) {
             y++;
         }
         return y;
     }
 
-    private static double dayWithinYear(final double t, final double year) {
-        return day(t) - dayFromYear(year);
+    private static int dayWithinYear(final double t, final int year) {
+        return (int) (day(t) - dayFromYear(year));
     }
 
-    private static double monthFromTime(final double t) {
-        final double year = yearFromTime(t);
-        final double day = dayWithinYear(t, year);
-        final int[] firstDay = firstDayInMonth[isLeapYear((int) year) ? 1 : 0];
+    private static int monthFromTime(final double t) {
+        final int year = yearFromTime(t);
+        final int day = dayWithinYear(t, year);
+        final int[] firstDay = firstDayInMonth[isLeapYear(year) ? 1 : 0];
         int month = 0;
 
         while (month < 11 && firstDay[month + 1] <= day) {
@@ -1103,10 +1104,10 @@
         return month;
     }
 
-    private static double dayFromTime(final double t)  {
-        final double year = yearFromTime(t);
-        final double day = dayWithinYear(t, year);
-        final int[] firstDay = firstDayInMonth[isLeapYear((int) year) ? 1 : 0];
+    private static int dayFromTime(final double t)  {
+        final int year = yearFromTime(t);
+        final int day = dayWithinYear(t, year);
+        final int[] firstDay = firstDayInMonth[isLeapYear(year) ? 1 : 0];
         int month = 0;
 
         while (month < 11 && firstDay[month + 1] <= day) {
@@ -1121,11 +1122,8 @@
         return firstDay[month];
     }
 
-    private static double weekDay(final double time) {
-        if (isNaN(time)) {
-            return NaN;
-        }
-        final double day = (day(time) + 4) % 7;
+    private static int weekDay(final double time) {
+        final int day = (int) (day(time) + 4) % 7;
         return day < 0 ? day + 7 : day;
     }
 
@@ -1140,26 +1138,26 @@
     }
 
     // ECMA 15.9.1.10 Hours, Minutes, Second, and Milliseconds
-    private static double hourFromTime(final double t) {
-        final double h = Math.floor(t / msPerHour) % hoursPerDay;
+    private static int hourFromTime(final double t) {
+        final int h = (int) (Math.floor(t / msPerHour) % hoursPerDay);
         return h < 0 ? h + hoursPerDay: h;
     }
-    private static double minFromTime(final double t) {
-        final double m = Math.floor(t / msPerMinute) % minutesPerHour;
+    private static int minFromTime(final double t) {
+        final int m = (int) (Math.floor(t / msPerMinute) % minutesPerHour);
         return m < 0 ? m + minutesPerHour : m;
     }
 
-    private static double secFromTime(final double t) {
-        final double s = Math.floor(t / msPerSecond) % secondsPerMinute;
+    private static int secFromTime(final double t) {
+        final int s = (int) (Math.floor(t / msPerSecond) % secondsPerMinute);
         return s < 0 ? s + secondsPerMinute : s;
     }
 
-    private static double msFromTime(final double t) {
-        final double m = t % msPerSecond;
+    private static int msFromTime(final double t) {
+        final int m = (int) (t % msPerSecond);
         return m < 0 ? m + msPerSecond : m;
     }
 
-    private static double valueFromTime(final int unit, final double t) {
+    private static int valueFromTime(final int unit, final double t) {
         switch (unit) {
             case YEAR: return yearFromTime(t);
             case MONTH: return monthFromTime(t);
@@ -1180,12 +1178,12 @@
     // ECMA 15.9.1.12 MakeDay (year, month, date)
     private static double makeDay(final double year, final double month, final double date) {
         final double y = year + Math.floor(month / 12);
-        double m = month % 12;
+        int m = (int) (month % 12);
         if (m < 0) {
             m += 12;
         }
-        double d = Math.floor(dayFromYear(y));
-        d += dayFromMonth((int) m, (int) y);
+        double d = dayFromYear(y);
+        d += dayFromMonth(m, (int) y);
 
         return d + date - 1;
     }
@@ -1257,13 +1255,13 @@
                     nullReturn = true;
                 }
 
-                if (! nullReturn) {
+                if (!nullReturn && !isNaN(time)) {
                     d[i - start] = valueFromTime(i, time);
                 }
             }
         }
 
-        return nullReturn? null : d;
+        return nullReturn ? null : d;
     }
 
     // ECMA 15.9.1.14 TimeClip (time)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/script/basic/JDK-8025163.js	Sat Sep 21 10:11:15 2013 +0200
@@ -0,0 +1,39 @@
+/*
+ * 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-8025163: Date methods should not return -0
+ *
+ * @test
+ * @run
+ */
+
+print(1 / (new Date(0, 0, 1)).getYear());
+print(1 / (new Date(1969, 1, 2)).getDay());
+print(1 / (new Date(1969, 0, 1)).getHours());
+print(1 / (new Date(1969, 0, 1)).getHours());
+print(1 / (new Date(1969, 0, 1)).getMinutes());
+print(1 / (new Date(1969, 0, 1)).getSeconds());
+print(1 / (new Date(1969, 0, 1)).getMilliseconds());
+print(1 / (new Date(1969, 0, 1)).getMilliseconds());
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/script/basic/JDK-8025163.js.EXPECTED	Sat Sep 21 10:11:15 2013 +0200
@@ -0,0 +1,8 @@
+Infinity
+Infinity
+Infinity
+Infinity
+Infinity
+Infinity
+Infinity
+Infinity