TimeParsing.java revision 0:37a05a11f281
1/*
2 * Copyright 2002-2003 Sun Microsystems, Inc.  All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4558835 4915146
27 * @summary Verify timezone offset and fractional seconds are correctly parsed
28 */
29
30import java.io.*;
31import java.util.Date;
32
33import sun.security.util.DerInputStream;
34
35public class TimeParsing {
36
37  // 10 Aug 2001 17:43:51 GMT
38  private final static long TIME = 997465431000l;
39  private final static long TIME_FRACT1 = 997465431700l;
40  private final static long TIME_FRACT2 = 997465431760l;
41  private final static long TIME_FRACT3 = 997465431765l;
42
43  // 010810174351Z
44  private final static byte[] UTC_ZULU =
45    {0x17, 0x0d, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x5a};
46
47  // 010810184351+0100
48  private final static byte[] UTC_PLUS1 =
49    {0x17, 0x11, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2b, 0x30, 0x31, 0x30, 0x30};
50
51  // 010810164351-0100
52  private final static byte[] UTC_MINUS1 =
53    {0x17, 0x11, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x36, 0x34, 0x33, 0x35, 0x31, 0x2d, 0x30, 0x31, 0x30, 0x30};
54
55  // 20010810174351Z
56  private final static byte[] GEN_ZULU =
57    {0x18, 0x0f, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x5a};
58
59  // 20010810184351+0100
60  private final static byte[] GEN_PLUS1 =
61    {0x18, 0x13, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2b, 0x30, 0x31, 0x30, 0x30};
62
63  // 20010810164351-0100
64  private final static byte[] GEN_MINUS1 =
65    {0x18, 0x13, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x36, 0x34, 0x33, 0x35, 0x31, 0x2d, 0x30, 0x31, 0x30, 0x30};
66
67  // 20010810174351.7Z
68  private final static byte[] GEN_FRACT1_ZULU =
69    {0x18, 0x11, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x5a};
70
71  // 20010810174351.76Z
72  private final static byte[] GEN_FRACT2_ZULU =
73    {0x18, 0x12, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x36, 0x5a};
74
75  // 20010810174351.765Z
76  private final static byte[] GEN_FRACT3_ZULU =
77    {0x18, 0x13, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x37, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x36, 0x35, 0x5a};
78
79  // 20010810184351.7+0100
80  private final static byte[] GEN_FRACT1_PLUS1 =
81    {0x18, 0x15, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x2b, 0x30, 0x31, 0x30, 0x30};
82
83  // 20010810184351.76+0100
84  private final static byte[] GEN_FRACT2_PLUS1 =
85    {0x18, 0x16, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x36, 0x2b, 0x30, 0x31, 0x30, 0x30};
86
87  // 20010810184351.765+0100
88  private final static byte[] GEN_FRACT3_PLUS1 =
89    {0x18, 0x17, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2e, 0x37, 0x36, 0x35, 0x2b, 0x30, 0x31, 0x30, 0x30};
90
91  // 20010810184351,765+0100
92  private final static byte[] GEN_FRACT3_COMMA_PLUS1 =
93    {0x18, 0x17, 0x32, 0x30, 0x30, 0x31, 0x30, 0x38, 0x31, 0x30, 0x31, 0x38, 0x34, 0x33, 0x35, 0x31, 0x2c, 0x37, 0x36, 0x35, 0x2b, 0x30, 0x31, 0x30, 0x30};
94
95
96  private static Date decodeUTC(byte[] b) throws IOException {
97    DerInputStream derin = new DerInputStream(b);
98    return derin.getUTCTime();
99  }
100
101  private static Date decodeGeneralized(byte[] b) throws IOException {
102    DerInputStream derin = new DerInputStream(b);
103    return derin.getGeneralizedTime();
104  }
105
106  private static  void checkUTC(Date d0, byte[] b, String text) throws Exception {
107    Date d1 = decodeUTC(b);
108    if( !d0.equals(d1) ) {
109      throw new Exception("UTCTime " + text + " failed: " + d1.toGMTString());
110    } else {
111      System.out.println("UTCTime " + text + " ok");
112    }
113  }
114
115  private static  void checkGeneralized(Date d0, byte[] b, String text) throws Exception {
116    Date d1 = decodeGeneralized(b);
117    if( !d0.equals(d1) ) {
118      throw new Exception("GeneralizedTime " + text + " failed: " + d1.toGMTString());
119    } else {
120      System.out.println("GeneralizedTime " + text + " ok");
121    }
122  }
123
124  public static void main(String args[]) throws Exception {
125    Date d0 = new Date(TIME);
126    System.out.println(d0.toGMTString());
127
128    checkUTC(d0, UTC_ZULU, "Zulu");
129    checkUTC(d0, UTC_PLUS1, "+0100");
130    checkUTC(d0, UTC_MINUS1, "-0100");
131
132    checkGeneralized(d0, GEN_ZULU, "Zulu");
133    checkGeneralized(d0, GEN_PLUS1, "+0100");
134    checkGeneralized(d0, GEN_MINUS1, "-0100");
135
136    Date d1 = new Date(TIME_FRACT1);
137    checkGeneralized(d1, GEN_FRACT1_ZULU, "fractional seconds (Zulu)");
138    checkGeneralized(d1, GEN_FRACT1_PLUS1, "fractional seconds (+0100)");
139
140    Date d2 = new Date(TIME_FRACT2);
141    checkGeneralized(d2, GEN_FRACT2_ZULU, "fractional seconds (Zulu)");
142    checkGeneralized(d2, GEN_FRACT2_PLUS1, "fractional seconds (+0100)");
143
144    Date d3 = new Date(TIME_FRACT3);
145    checkGeneralized(d3, GEN_FRACT3_ZULU, "fractional seconds (Zulu)");
146    checkGeneralized(d3, GEN_FRACT3_PLUS1, "fractional seconds (+0100)");
147    checkGeneralized(d3, GEN_FRACT3_COMMA_PLUS1, "fractional seconds (+0100)");
148  }
149
150}
151