initial checkin
[org.ibex.nanogoat.git] / src / org / bouncycastle / asn1 / DERUTCTime.java
1 package org.bouncycastle.asn1;
2
3 import java.util.*;
4 import java.io.*;
5 import java.text.*;
6
7 /**
8  * UTC time object.
9  */
10 public class DERUTCTime
11     extends DERObject
12 {
13     String      time;
14
15     /**
16      * return an UTC Time from the passed in object.
17      *
18      * @exception IllegalArgumentException if the object cannot be converted.
19      */
20     public static DERUTCTime getInstance(
21         Object  obj)
22     {
23         if (obj == null || obj instanceof DERUTCTime)
24         {
25             return (DERUTCTime)obj;
26         }
27
28         if (obj instanceof ASN1OctetString)
29         {
30             return new DERUTCTime(((ASN1OctetString)obj).getOctets());
31         }
32
33         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
34     }
35
36     /**
37      * return an UTC Time from a tagged object.
38      *
39      * @param obj the tagged object holding the object we want
40      * @param explicit true if the object is meant to be explicitly
41      *              tagged false otherwise.
42      * @exception IllegalArgumentException if the tagged object cannot
43      *               be converted.
44      */
45     public static DERUTCTime getInstance(
46         ASN1TaggedObject obj,
47         boolean          explicit)
48     {
49         return getInstance(obj.getObject());
50     }
51     
52     /**
53      * The correct format for this is YYMMDDHHMMSSZ (it used to be that seconds were
54      * never encoded. When you're creating one of these objects from scratch, that's
55      * what you want to use, otherwise we'll try to deal with whatever gets read from
56      * the input stream... (this is why the input format is different from the getTime()
57      * method output).
58      * <p>
59      *
60      * @param time the time string.
61      */
62     public DERUTCTime(
63         String  time)
64     {
65         this.time = time;
66     }
67
68     /**
69      * base constructer from a java.util.date object
70      */
71     public DERUTCTime(
72        Date time)
73     {
74         SimpleDateFormat dateF = new SimpleDateFormat("yyMMddHHmmss'Z'");
75
76         dateF.setTimeZone(new SimpleTimeZone(0,"Z"));
77
78         this.time = dateF.format(time);
79     }
80
81     DERUTCTime(
82         byte[]  bytes)
83     {
84         //
85         // explicitly convert to characters
86         //
87         char[]  dateC = new char[bytes.length];
88
89         for (int i = 0; i != dateC.length; i++)
90         {
91             dateC[i] = (char)(bytes[i] & 0xff);
92         }
93
94         this.time = new String(dateC);
95     }
96
97     /**
98      * return the time - always in the form of 
99      *  YYMMDDhhmmssGMT(+hh:mm|-hh:mm).
100      * <p>
101      * Normally in a certificate we would expect "Z" rather than "GMT",
102      * however adding the "GMT" means we can just use:
103      * <pre>
104      *     dateF = new SimpleDateFormat("yyMMddHHmmssz");
105      * </pre>
106      * To read in the time and get a date which is compatible with our local
107      * time zone.
108      * <p>
109      * <b>Note:</b> In some cases, due to the local date processing, this
110      * may lead to unexpected results. If you want to stick the normal
111      * convention of 1950 to 2049 use the getAdjustedTime() method.
112      */
113     public String getTime()
114     {
115         //
116         // standardise the format.
117         //
118         if (time.length() == 11)
119         {
120             return time.substring(0, 10) + "00GMT+00:00";
121         }
122         else if (time.length() == 13)
123         {
124             return time.substring(0, 12) + "GMT+00:00";
125         }
126         else if (time.length() == 17)
127         {
128             return time.substring(0, 12) + "GMT" + time.substring(12, 15) + ":" + time.substring(15, 17);
129         }
130
131         return time;
132     }
133
134     /**
135      * return the time as an adjusted date with a 4 digit year. This goes
136      * in the range of 1950 - 2049.
137      */
138     public String getAdjustedTime()
139     {
140         String   d = this.getTime();
141
142         if (d.charAt(0) < '5')
143         {
144             return "20" + d;
145         }
146         else
147         {
148             return "19" + d;
149         }
150     }
151
152     private byte[] getOctets()
153     {
154         char[]  cs = time.toCharArray();
155         byte[]  bs = new byte[cs.length];
156
157         for (int i = 0; i != cs.length; i++)
158         {
159             bs[i] = (byte)cs[i];
160         }
161
162         return bs;
163     }
164
165     void encode(
166         DEROutputStream  out)
167         throws IOException
168     {
169         out.writeEncoded(UTC_TIME, this.getOctets());
170     }
171     
172     public boolean equals(
173         Object  o)
174     {
175         if ((o == null) || !(o instanceof DERUTCTime))
176         {
177             return false;
178         }
179
180         return time.equals(((DERUTCTime)o).time);
181     }
182 }