1 package org.bouncycastle.asn1;
2
3 import java.io.*;
4 import java.util.*;
5 import java.io.*;
6 import java.text.*;
7
8
11 public class DERUTCTime
12 extends DERObject
13 {
14 String time;
15
16
21 public static DERUTCTime getInstance(
22 Object obj)
23 {
24 if (obj == null || obj instanceof DERUTCTime)
25 {
26 return (DERUTCTime)obj;
27 }
28
29 if (obj instanceof ASN1OctetString)
30 {
31 return new DERUTCTime(((ASN1OctetString)obj).getOctets());
32 }
33
34 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
35 }
36
37
46 public static DERUTCTime getInstance(
47 ASN1TaggedObject obj,
48 boolean explicit)
49 {
50 return getInstance(obj.getObject());
51 }
52
53
63 public DERUTCTime(
64 String time)
65 {
66 this.time = time;
67 }
68
69
72 public DERUTCTime(
73 Date time)
74 {
75 SimpleDateFormat dateF = new SimpleDateFormat("yyMMddHHmmss'Z'");
76
77 dateF.setTimeZone(new SimpleTimeZone(0,"Z"));
78
79 this.time = dateF.format(time);
80 }
81
82 DERUTCTime(
83 byte[] bytes)
84 {
85
86
87
88 char[] dateC = new char[bytes.length];
89
90 for (int i = 0; i != dateC.length; i++)
91 {
92 dateC[i] = (char)(bytes[i] & 0xff);
93 }
94
95 this.time = new String(dateC);
96 }
97
98
114 public String getTime()
115 {
116
117
118
119 if (time.length() == 11)
120 {
121 return time.substring(0, 10) + "00GMT+00:00";
122 }
123 else if (time.length() == 13)
124 {
125 return time.substring(0, 12) + "GMT+00:00";
126 }
127 else if (time.length() == 17)
128 {
129 return time.substring(0, 12) + "GMT" + time.substring(12, 15) + ":" + time.substring(15, 17);
130 }
131
132 return time;
133 }
134
135
139 public String getAdjustedTime()
140 {
141 String d = this.getTime();
142
143 if (d.charAt(0) < '5')
144 {
145 return "20" + d;
146 }
147 else
148 {
149 return "19" + d;
150 }
151 }
152
153 private byte[] getOctets()
154 {
155 char[] cs = time.toCharArray();
156 byte[] bs = new byte[cs.length];
157
158 for (int i = 0; i != cs.length; i++)
159 {
160 bs[i] = (byte)cs[i];
161 }
162
163 return bs;
164 }
165
166 void encode(
167 DEROutputStream out)
168 throws IOException
169 {
170 out.writeEncoded(UTC_TIME, this.getOctets());
171 }
172
173 public boolean equals(
174 Object o)
175 {
176 if ((o == null) || !(o instanceof DERUTCTime))
177 {
178 return false;
179 }
180
181 return time.equals(((DERUTCTime)o).time);
182 }
183 }
184