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