resolve darcs stupidity
[org.ibex.core.git] / src / org / bouncycastle / asn1 / DERGeneralizedTime.java
1 package org.bouncycastle.asn1;
2
3 import java.util.*;
4 import java.io.*;
5 import java.text.*;
6
7 /**
8  * Generalized time object.
9  */
10 public class DERGeneralizedTime
11     extends DERObject
12 {
13     String      time;
14
15     /**
16      * return a generalized time from the passed in object
17      *
18      * @exception IllegalArgumentException if the object cannot be converted.
19      */
20     public static DERGeneralizedTime getInstance(
21         Object  obj)
22     {
23         if (obj == null || obj instanceof DERGeneralizedTime)
24         {
25             return (DERGeneralizedTime)obj;
26         }
27
28         if (obj instanceof ASN1OctetString)
29         {
30             return new DERGeneralizedTime(((ASN1OctetString)obj).getOctets());
31         }
32
33         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
34     }
35
36     /**
37      * return a Generalized Time object 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 DERGeneralizedTime getInstance(
46         ASN1TaggedObject obj,
47         boolean          explicit)
48     {
49         return getInstance(obj.getObject());
50     }
51     
52     /**
53      * The correct format for this is YYYYMMDDHHMMSSZ, or without the Z
54      * for local time, or Z+-HHMM on the end, for difference between local
55      * time and UTC time.
56      * <p>
57      *
58      * @param time the time string.
59      */
60     public DERGeneralizedTime(
61         String  time)
62     {
63         this.time = time;
64     }
65
66     /**
67      * base constructer from a java.util.date object
68      */
69     public DERGeneralizedTime(
70         Date time)
71     {
72         SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmss'Z'");
73
74         dateF.setTimeZone(new SimpleTimeZone(0,"Z"));
75
76         this.time = dateF.format(time);
77     }
78
79     DERGeneralizedTime(
80         byte[]  bytes)
81     {
82         //
83         // explicitly convert to characters
84         //
85         char[]  dateC = new char[bytes.length];
86
87         for (int i = 0; i != dateC.length; i++)
88         {
89             dateC[i] = (char)(bytes[i] & 0xff);
90         }
91
92         this.time = new String(dateC);
93     }
94
95     /**
96      * return the time - always in the form of 
97      *  YYYYMMDDhhmmssGMT(+hh:mm|-hh:mm).
98      * <p>
99      * Normally in a certificate we would expect "Z" rather than "GMT",
100      * however adding the "GMT" means we can just use:
101      * <pre>
102      *     dateF = new SimpleDateFormat("yyyyMMddHHmmssz");
103      * </pre>
104      * To read in the time and get a date which is compatible with our local
105      * time zone.
106      */
107     public String getTime()
108     {
109         //
110         // standardise the format.
111         //
112         if (time.length() == 15)
113         {
114             return time.substring(0, 14) + "GMT+00:00";
115         }
116         else if (time.length() == 17)
117         {
118             return time.substring(0, 14) + "GMT" + time.substring(15, 17) + ":" + time.substring(17, 19);
119         }
120
121         return time;
122     }
123
124     private byte[] getOctets()
125     {
126         char[]  cs = time.toCharArray();
127         byte[]  bs = new byte[cs.length];
128
129         for (int i = 0; i != cs.length; i++)
130         {
131             bs[i] = (byte)cs[i];
132         }
133
134         return bs;
135     }
136
137
138     void encode(
139         DEROutputStream  out)
140         throws IOException
141     {
142         out.writeEncoded(GENERALIZED_TIME, this.getOctets());
143     }
144     
145     public boolean equals(
146         Object  o)
147     {
148         if ((o == null) || !(o instanceof DERGeneralizedTime))
149         {
150             return false;
151         }
152
153         return time.equals(((DERGeneralizedTime)o).time);
154     }
155 }