001: package org.bouncycastle.asn1;
002:
003: import java.io.IOException;
004: import java.text.ParseException;
005: import java.text.SimpleDateFormat;
006: import java.util.Date;
007: import java.util.SimpleTimeZone;
008:
009: /**
010: * UTC time object.
011: */
012: public class DERUTCTime extends ASN1Object {
013: String time;
014:
015: /**
016: * return an UTC Time from the passed in object.
017: *
018: * @exception IllegalArgumentException if the object cannot be converted.
019: */
020: public static DERUTCTime getInstance(Object obj) {
021: if (obj == null || obj instanceof DERUTCTime) {
022: return (DERUTCTime) obj;
023: }
024:
025: if (obj instanceof ASN1OctetString) {
026: return new DERUTCTime(((ASN1OctetString) obj).getOctets());
027: }
028:
029: throw new IllegalArgumentException(
030: "illegal object in getInstance: "
031: + obj.getClass().getName());
032: }
033:
034: /**
035: * return an UTC Time from a tagged object.
036: *
037: * @param obj the tagged object holding the object we want
038: * @param explicit true if the object is meant to be explicitly
039: * tagged false otherwise.
040: * @exception IllegalArgumentException if the tagged object cannot
041: * be converted.
042: */
043: public static DERUTCTime getInstance(ASN1TaggedObject obj,
044: boolean explicit) {
045: return getInstance(obj.getObject());
046: }
047:
048: /**
049: * The correct format for this is YYMMDDHHMMSSZ (it used to be that seconds were
050: * never encoded. When you're creating one of these objects from scratch, that's
051: * what you want to use, otherwise we'll try to deal with whatever gets read from
052: * the input stream... (this is why the input format is different from the getTime()
053: * method output).
054: * <p>
055: *
056: * @param time the time string.
057: */
058: public DERUTCTime(String time) {
059: this .time = time;
060: try {
061: this .getDate();
062: } catch (ParseException e) {
063: throw new IllegalArgumentException("invalid date string: "
064: + e.getMessage());
065: }
066: }
067:
068: /**
069: * base constructer from a java.util.date object
070: */
071: public DERUTCTime(Date time) {
072: SimpleDateFormat dateF = new SimpleDateFormat("yyMMddHHmmss'Z'");
073:
074: dateF.setTimeZone(new SimpleTimeZone(0, "Z"));
075:
076: this .time = dateF.format(time);
077: }
078:
079: DERUTCTime(byte[] bytes) {
080: //
081: // explicitly convert to characters
082: //
083: char[] dateC = new char[bytes.length];
084:
085: for (int i = 0; i != dateC.length; i++) {
086: dateC[i] = (char) (bytes[i] & 0xff);
087: }
088:
089: this .time = new String(dateC);
090: }
091:
092: /**
093: * return the time as a date based on whatever a 2 digit year will return. For
094: * standardised processing use getAdjustedDate().
095: *
096: * @return the resulting date
097: * @exception ParseException if the date string cannot be parsed.
098: */
099: public Date getDate() throws ParseException {
100: SimpleDateFormat dateF = new SimpleDateFormat("yyMMddHHmmssz");
101:
102: return dateF.parse(getTime());
103: }
104:
105: /**
106: * return the time as an adjusted date
107: * in the range of 1950 - 2049.
108: *
109: * @return a date in the range of 1950 to 2049.
110: * @exception ParseException if the date string cannot be parsed.
111: */
112: public Date getAdjustedDate() throws ParseException {
113: SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmssz");
114:
115: dateF.setTimeZone(new SimpleTimeZone(0, "Z"));
116:
117: return dateF.parse(getAdjustedTime());
118: }
119:
120: /**
121: * return the time - always in the form of
122: * YYMMDDhhmmssGMT(+hh:mm|-hh:mm).
123: * <p>
124: * Normally in a certificate we would expect "Z" rather than "GMT",
125: * however adding the "GMT" means we can just use:
126: * <pre>
127: * dateF = new SimpleDateFormat("yyMMddHHmmssz");
128: * </pre>
129: * To read in the time and get a date which is compatible with our local
130: * time zone.
131: * <p>
132: * <b>Note:</b> In some cases, due to the local date processing, this
133: * may lead to unexpected results. If you want to stick the normal
134: * convention of 1950 to 2049 use the getAdjustedTime() method.
135: */
136: public String getTime() {
137: //
138: // standardise the format.
139: //
140: if (time.indexOf('-') < 0 && time.indexOf('+') < 0) {
141: if (time.length() == 11) {
142: return time.substring(0, 10) + "00GMT+00:00";
143: } else {
144: return time.substring(0, 12) + "GMT+00:00";
145: }
146: } else {
147: int index = time.indexOf('-');
148: if (index < 0) {
149: index = time.indexOf('+');
150: }
151: String d = time;
152:
153: if (index == time.length() - 3) {
154: d += "00";
155: }
156:
157: if (index == 10) {
158: return d.substring(0, 10) + "00GMT"
159: + d.substring(10, 13) + ":"
160: + d.substring(13, 15);
161: } else {
162: return d.substring(0, 12) + "GMT" + d.substring(12, 15)
163: + ":" + d.substring(15, 17);
164: }
165: }
166: }
167:
168: /**
169: * return a time string as an adjusted date with a 4 digit year. This goes
170: * in the range of 1950 - 2049.
171: */
172: public String getAdjustedTime() {
173: String d = this .getTime();
174:
175: if (d.charAt(0) < '5') {
176: return "20" + d;
177: } else {
178: return "19" + d;
179: }
180: }
181:
182: private byte[] getOctets() {
183: char[] cs = time.toCharArray();
184: byte[] bs = new byte[cs.length];
185:
186: for (int i = 0; i != cs.length; i++) {
187: bs[i] = (byte) cs[i];
188: }
189:
190: return bs;
191: }
192:
193: void encode(DEROutputStream out) throws IOException {
194: out.writeEncoded(UTC_TIME, this .getOctets());
195: }
196:
197: boolean asn1Equals(DERObject o) {
198: if (!(o instanceof DERUTCTime)) {
199: return false;
200: }
201:
202: return time.equals(((DERUTCTime) o).time);
203: }
204:
205: public int hashCode() {
206: return time.hashCode();
207: }
208:
209: public String toString() {
210: return time;
211: }
212: }
|