1package org.bouncycastle.asn1;
2
3import java.io.IOException;
4import java.text.ParseException;
5import java.text.SimpleDateFormat;
6import java.util.Date;
7import java.util.SimpleTimeZone;
8
9/**
10 * UTC time object.
11 */
12public class DERUTCTime
13    extends ASN1Object
14{
15    String      time;
16
17    /**
18     * return an UTC Time from the passed in object.
19     *
20     * @exception IllegalArgumentException if the object cannot be converted.
21     */
22    public static DERUTCTime getInstance(
23        Object  obj)
24    {
25        if (obj == null || obj instanceof DERUTCTime)
26        {
27            return (DERUTCTime)obj;
28        }
29
30        throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
31    }
32
33    /**
34     * return an UTC Time from a tagged object.
35     *
36     * @param obj the tagged object holding the object we want
37     * @param explicit true if the object is meant to be explicitly
38     *              tagged false otherwise.
39     * @exception IllegalArgumentException if the tagged object cannot
40     *               be converted.
41     */
42    public static DERUTCTime getInstance(
43        ASN1TaggedObject obj,
44        boolean          explicit)
45    {
46        DERObject o = obj.getObject();
47
48        if (explicit || o instanceof DERUTCTime)
49        {
50            return getInstance(o);
51        }
52        else
53        {
54            return new DERUTCTime(((ASN1OctetString)o).getOctets());
55        }
56    }
57
58    /**
59     * The correct format for this is YYMMDDHHMMSSZ (it used to be that seconds were
60     * never encoded. When you're creating one of these objects from scratch, that's
61     * what you want to use, otherwise we'll try to deal with whatever gets read from
62     * the input stream... (this is why the input format is different from the getTime()
63     * method output).
64     * <p>
65     *
66     * @param time the time string.
67     */
68    public DERUTCTime(
69        String  time)
70    {
71        this.time = time;
72        try
73        {
74            this.getDate();
75        }
76        catch (ParseException e)
77        {
78            throw new IllegalArgumentException("invalid date string: " + e.getMessage());
79        }
80    }
81
82    /**
83     * base constructer from a java.util.date object
84     */
85    public DERUTCTime(
86        Date time)
87    {
88        SimpleDateFormat dateF = new SimpleDateFormat("yyMMddHHmmss'Z'");
89
90        dateF.setTimeZone(new SimpleTimeZone(0,"Z"));
91
92        this.time = dateF.format(time);
93    }
94
95    DERUTCTime(
96        byte[]  bytes)
97    {
98        //
99        // explicitly convert to characters
100        //
101        char[]  dateC = new char[bytes.length];
102
103        for (int i = 0; i != dateC.length; i++)
104        {
105            dateC[i] = (char)(bytes[i] & 0xff);
106        }
107
108        this.time = new String(dateC);
109    }
110
111    /**
112     * return the time as a date based on whatever a 2 digit year will return. For
113     * standardised processing use getAdjustedDate().
114     *
115     * @return the resulting date
116     * @exception ParseException if the date string cannot be parsed.
117     */
118    public Date getDate()
119        throws ParseException
120    {
121        SimpleDateFormat dateF = new SimpleDateFormat("yyMMddHHmmssz");
122
123        return dateF.parse(getTime());
124    }
125
126    /**
127     * return the time as an adjusted date
128     * in the range of 1950 - 2049.
129     *
130     * @return a date in the range of 1950 to 2049.
131     * @exception ParseException if the date string cannot be parsed.
132     */
133    public Date getAdjustedDate()
134        throws ParseException
135    {
136        SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmssz");
137
138        dateF.setTimeZone(new SimpleTimeZone(0, "Z"));
139
140        return dateF.parse(getAdjustedTime());
141    }
142
143    /**
144     * return the time - always in the form of
145     *  YYMMDDhhmmssGMT(+hh:mm|-hh:mm).
146     * <p>
147     * Normally in a certificate we would expect "Z" rather than "GMT",
148     * however adding the "GMT" means we can just use:
149     * <pre>
150     *     dateF = new SimpleDateFormat("yyMMddHHmmssz");
151     * </pre>
152     * To read in the time and get a date which is compatible with our local
153     * time zone.
154     * <p>
155     * <b>Note:</b> In some cases, due to the local date processing, this
156     * may lead to unexpected results. If you want to stick the normal
157     * convention of 1950 to 2049 use the getAdjustedTime() method.
158     */
159    public String getTime()
160    {
161        //
162        // standardise the format.
163        //
164        if (time.indexOf('-') < 0 && time.indexOf('+') < 0)
165        {
166            if (time.length() == 11)
167            {
168                return time.substring(0, 10) + "00GMT+00:00";
169            }
170            else
171            {
172                return time.substring(0, 12) + "GMT+00:00";
173            }
174        }
175        else
176        {
177            int index = time.indexOf('-');
178            if (index < 0)
179            {
180                index = time.indexOf('+');
181            }
182            String d = time;
183
184            if (index == time.length() - 3)
185            {
186                d += "00";
187            }
188
189            if (index == 10)
190            {
191                return d.substring(0, 10) + "00GMT" + d.substring(10, 13) + ":" + d.substring(13, 15);
192            }
193            else
194            {
195                return d.substring(0, 12) + "GMT" + d.substring(12, 15) + ":" +  d.substring(15, 17);
196            }
197        }
198    }
199
200    /**
201     * return a time string as an adjusted date with a 4 digit year. This goes
202     * in the range of 1950 - 2049.
203     */
204    public String getAdjustedTime()
205    {
206        String   d = this.getTime();
207
208        if (d.charAt(0) < '5')
209        {
210            return "20" + d;
211        }
212        else
213        {
214            return "19" + d;
215        }
216    }
217
218    private byte[] getOctets()
219    {
220        char[]  cs = time.toCharArray();
221        byte[]  bs = new byte[cs.length];
222
223        for (int i = 0; i != cs.length; i++)
224        {
225            bs[i] = (byte)cs[i];
226        }
227
228        return bs;
229    }
230
231    void encode(
232        DEROutputStream  out)
233        throws IOException
234    {
235        out.writeEncoded(UTC_TIME, this.getOctets());
236    }
237
238    boolean asn1Equals(
239        DERObject  o)
240    {
241        if (!(o instanceof DERUTCTime))
242        {
243            return false;
244        }
245
246        return time.equals(((DERUTCTime)o).time);
247    }
248
249    public int hashCode()
250    {
251        return time.hashCode();
252    }
253
254    public String toString()
255    {
256      return time;
257    }
258}
259