2002/03/21 01:19:32
[org.ibex.core.git] / src / org / bouncycastle / asn1 / DERUTCTime.java
1 package org.bouncycastle.asn1;
2
3 import java.io.*;
4 import java.util.*;
5 import java.io.*;
6
7 /**
8  * UTC time object.
9  */
10 public class DERUTCTime
11     extends DERObject
12 {
13     String      time;
14
15     /**
16      * The correct format for this is YYMMDDHHMMSSZ (it used to be that seconds were
17      * never encoded. When you're creating one of these objects from scratch, that's
18      * what you want to use, otherwise we'll try to deal with whatever gets read from
19      * the input stream... (this is why the input format is different from the getTime()
20      * method output).
21      * <p>
22      * You can generate a Java date string in the right format by using:
23      * <pre>
24      *      dateF = new SimpleDateFormat("yyMMddHHmmss");
25      *      tz = new SimpleTimeZone(0, "Z");
26      *     
27      *      dateF.setTimeZone(tz);
28      *
29      *      utcTime = new DERUTCTime(dateF.format(new Date()) + "Z");
30      * </pre>
31      *
32      * @param time the time string.
33      */
34     public DERUTCTime(
35         String  time)
36     {
37         this.time = time;
38     }
39
40     /**
41      * return the time - always in the form of 
42      *  YYMMDDhhmmssGMT(+hh:mm|-hh:mm).
43      * <p>
44      * Normally in a certificate we would expect "Z" rather than "GMT",
45      * however adding the "GMT" means we can just use:
46      * <pre>
47      *     dateF = new SimpleDateFormat("yyMMddHHmmssz");
48      * </pre>
49      * To read in the time and get a date which is compatible with our local
50      * time zone.
51      */
52     public String getTime()
53     {
54         //
55         // standardise the format.
56         //
57         if (time.length() == 11)
58         {
59             return time.substring(0, 10) + "00GMT+00:00";
60         }
61         else if (time.length() == 13)
62         {
63             return time.substring(0, 12) + "GMT+00:00";
64         }
65         else if (time.length() == 17)
66         {
67             return time.substring(0, 12) + "GMT" + time.substring(12, 15) + ":" + time.substring(15, 17);
68         }
69
70         return time;
71     }
72
73     void encode(
74         DEROutputStream  out)
75         throws IOException
76     {
77         out.writeEncoded(UTC_TIME, time.getBytes());
78     }
79 }