harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From smish...@apache.org
Subject svn commit: r433987 - in /incubator/harmony/enhanced/classlib/trunk/modules/security/src: main/java/common/org/apache/harmony/security/asn1/ASN1Time.java test/impl/java/org/apache/harmony/security/tests/asn1/der/GeneralizedTimeTest.java
Date Wed, 23 Aug 2006 09:09:49 GMT
Author: smishura
Date: Wed Aug 23 02:09:49 2006
New Revision: 433987

URL: http://svn.apache.org/viewvc?rev=433987&view=rev
Log:
Fix for HARMONY-1252 ([classlib][security] ASN.1 framework incorrectly encodes the dates.)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/ASN1Time.java
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/asn1/der/GeneralizedTimeTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/ASN1Time.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/ASN1Time.java?rev=433987&r1=433986&r2=433987&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/ASN1Time.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/org/apache/harmony/security/asn1/ASN1Time.java
Wed Aug 23 02:09:49 2006
@@ -22,7 +22,9 @@
 package org.apache.harmony.security.asn1;
 
 import java.io.IOException;
-import java.util.Date;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.TimeZone;
 
 
 /**
@@ -33,10 +35,6 @@
 
 public abstract class ASN1Time extends ASN1StringType {
 
-    // The number of days in monthes
-    protected static final byte[] DAYS = new byte[] { 31, 28, 31, 30, 31, 30,
-            31, 31, 30, 31, 30, 31 };
-
     /**
      * TODO Put ctor description here
      * 
@@ -47,45 +45,19 @@
     }
 
     public Object getDecodedObject(BerInputStream in) throws IOException {
-        long milliseconds = ASN1Time.getMilliseconds(in.times);
-        return new Date(milliseconds);
-
-    }
-
-    public static long getMilliseconds(int[] times) {
-        // count the number of milliseconds since Jan 1, 1970, 00:00:00 GMT
-        long res = times[6]; //milliseconds
-        res += times[5] * 1000; //second
-        res += (long) times[4] * 60000; //minute
-        res += (long) times[3] * 3600000; //hour
-        res += (long) (times[2]-1) * 86400000; //day
-        for (int i = 1; i < times[1]; i++) { //month
-            res += (long) DAYS[i - 1] * 86400000;
-        }
-        // the number of passed leap years without this year
-        int leap_years_num =
-            (times[0] > 1970) ? (times[0] - 1969)/4 : (times[0] - 1971)/4;
-        res += (times[0] - 1970) * 31536000000l 
-                            + (long) leap_years_num * 86400000;
-        // if this year is a leap year and this is time after the february
-        if ((times[0] % 4 == 0) && (times[1] > 2)) {
-            res += (times[0] > 1970) ? 86400000 : -86400000; 
-        }
-//        // count the number of milliseconds since Jan 1, 1970, 00:00:00 GMT
-//        long res = times[6]; //milliseconds
-//        res += times[5] * 1000; //second
-//        res += (long) times[4] * 60000; //minute
-//        res += (long) times[3] * 3600000; //hour
-//        res += (long) (times[2] - 1) * 86400000; //day
-//        for (int i = 1; i < times[1]; i++) { //month
-//            res += (long) DAYS[i - 1] * 86400000;
-//        }
-//        if ((times[0] % 4 == 0) && (times[1] > 2)) {
-//            res += (long) 86400000; // this year is a leap year
-//        }
-//        if (times[0] > 1970) {
-//            res += ((long) (times[0] - 1970) * 31536000000l + (long) ((times[0] - 1969)
/ 4) * 86400000);
-//        }
-        return res;
+        
+        // TODO optimize me:
+        // It makes sense use calendar instance instead of times array
+        GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
+        
+        c.set(Calendar.YEAR, in.times[0]);
+        c.set(Calendar.MONTH, in.times[1]-1);
+        c.set(Calendar.DAY_OF_MONTH, in.times[2]);
+        c.set(Calendar.HOUR_OF_DAY, in.times[3]);
+        c.set(Calendar.MINUTE, in.times[4]);
+        c.set(Calendar.SECOND, in.times[5]);
+        c.set(Calendar.MILLISECOND, in.times[6]);
+        
+        return c.getTime();
     }
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/asn1/der/GeneralizedTimeTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/asn1/der/GeneralizedTimeTest.java?rev=433987&r1=433986&r2=433987&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/asn1/der/GeneralizedTimeTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/asn1/der/GeneralizedTimeTest.java
Wed Aug 23 02:09:49 2006
@@ -17,9 +17,11 @@
 package org.apache.harmony.security.tests.asn1.der;
 
 import java.io.ByteArrayInputStream;
+import java.io.IOException;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Arrays;
+import java.util.Date;
 import java.util.Locale;
 import java.util.TimeZone;
 
@@ -72,7 +74,14 @@
                         "13 Dec 2050 14:15:16",
                         new byte[] { 0x18, 0x0F, 0x32, 0x30, 0x35, 0x30, 0x31,
                                 0x32, 0x31, 0x33, 0x31, 0x34, 0x31, 0x35, 0x31,
-                                0x36, 0x5A }, null }, };
+                                0x36, 0x5A }, null },
+                // YYYYMMDD-HHMMSS = "20501213141516Z"
+                {
+                        "29 Mar 2332 06:56:40",
+                        new byte[] { 0x18, 0x0F, 0x32, 0x33, 0x33, 0x32, 0x30,
+                                0x33, 0x32, 0x39, 0x30, 0x36, 0x35, 0x36, 0x34,
+                                0x30, 0x5A }, null },
+        };
 
         try {
             // fill values for Date objects by parsing date string
@@ -120,6 +129,17 @@
         }
     }
 
+    /**
+     * Tests milliseconds result of encoding/decoding on the date after 2050.
+     */
+    public void test_Milliseconds() throws IOException{
+        // Regression test for HARMONY-1252
+        long old_date = 11431151800000L;
+        long new_date = ((Date) gtime.decode(gtime.encode(new Date(old_date))))
+                .getTime();
+        assertEquals(old_date, new_date);
+    }
+    
     public static void main(String[] args) {
         junit.textui.TestRunner.run(GeneralizedTimeTest.class);
     }



Mime
View raw message