package org.bouncycastle.asn1.cms; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.SimpleTimeZone; import org.bouncycastle.asn1.ASN1Choice; import org.bouncycastle.asn1.ASN1Object; import org.bouncycastle.asn1.ASN1Primitive; import org.bouncycastle.asn1.ASN1TaggedObject; import org.bouncycastle.asn1.DERGeneralizedTime; import org.bouncycastle.asn1.DERUTCTime; /** * RFC 5652: * Dual-mode timestamp format producing either UTCTIme or GeneralizedTime. *
*
* Time ::= CHOICE { * utcTime UTCTime, * generalTime GeneralizedTime } **
* This has a constructor using java.util.Date for input which generates * a {@link org.bouncycastle.asn1.DERUTCTime DERUTCTime} object if the * supplied datetime is in range 1950-01-01-00:00:00 UTC until 2049-12-31-23:59:60 UTC. * If the datetime value is outside that range, the generated object will be * {@link org.bouncycastle.asn1.DERGeneralizedTime DERGeneralizedTime}. */ public class Time extends ASN1Object implements ASN1Choice { ASN1Primitive time; public static Time getInstance( ASN1TaggedObject obj, boolean explicit) { return getInstance(obj.getObject()); } /** * @deprecated use getInstance() */ public Time( ASN1Primitive time) { if (!(time instanceof DERUTCTime) && !(time instanceof DERGeneralizedTime)) { throw new IllegalArgumentException("unknown object passed to Time"); } this.time = time; } /** * Create a time object from a given date - if the year is in between 1950 * and 2049 a UTCTime object is generated, otherwise a GeneralizedTime * is used. */ public Time( Date date) { SimpleTimeZone tz = new SimpleTimeZone(0, "Z"); SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmss"); dateF.setTimeZone(tz); String d = dateF.format(date) + "Z"; int year = Integer.parseInt(d.substring(0, 4)); if (year < 1950 || year > 2049) { time = new DERGeneralizedTime(d); } else { time = new DERUTCTime(d.substring(2)); } } /** * Return a Time object from the given object. *
* Accepted inputs: *