// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.asn1;

import org.bouncycastle.util.Arrays;
import java.io.IOException;
import java.util.Locale;
import java.util.TimeZone;
import java.util.SimpleTimeZone;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;
import org.bouncycastle.util.Strings;

public class ASN1UTCTime extends ASN1Primitive
{
    static final ASN1UniversalType TYPE;
    final byte[] contents;
    
    public static ASN1UTCTime getInstance(final Object o) {
        if (o == null || o instanceof ASN1UTCTime) {
            return (ASN1UTCTime)o;
        }
        if (o instanceof ASN1Encodable) {
            final ASN1Primitive asn1Primitive = ((ASN1Encodable)o).toASN1Primitive();
            if (asn1Primitive instanceof ASN1UTCTime) {
                return (ASN1UTCTime)asn1Primitive;
            }
        }
        if (o instanceof byte[]) {
            try {
                return (ASN1UTCTime)ASN1UTCTime.TYPE.fromByteArray((byte[])o);
            }
            catch (final Exception ex) {
                throw new IllegalArgumentException("encoding error in getInstance: " + ex.toString());
            }
        }
        throw new IllegalArgumentException("illegal object in getInstance: " + o.getClass().getName());
    }
    
    public static ASN1UTCTime getInstance(final ASN1TaggedObject asn1TaggedObject, final boolean b) {
        return (ASN1UTCTime)ASN1UTCTime.TYPE.getContextTagged(asn1TaggedObject, b);
    }
    
    public static ASN1UTCTime getTagged(final ASN1TaggedObject asn1TaggedObject, final boolean b) {
        return (ASN1UTCTime)ASN1UTCTime.TYPE.getTagged(asn1TaggedObject, b);
    }
    
    public ASN1UTCTime(final String s) {
        this.contents = Strings.toByteArray(s);
        try {
            this.getDate();
        }
        catch (final ParseException ex) {
            throw new IllegalArgumentException("invalid date string: " + ex.getMessage());
        }
    }
    
    public ASN1UTCTime(final Date date) {
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMddHHmmss'Z'", LocaleUtil.EN_Locale);
        simpleDateFormat.setTimeZone(new SimpleTimeZone(0, "Z"));
        this.contents = Strings.toByteArray(simpleDateFormat.format(date));
    }
    
    public ASN1UTCTime(final Date date, final Locale locale) {
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMddHHmmss'Z'", locale);
        simpleDateFormat.setTimeZone(new SimpleTimeZone(0, "Z"));
        this.contents = Strings.toByteArray(simpleDateFormat.format(date));
    }
    
    ASN1UTCTime(final byte[] contents) {
        if (contents.length < 2) {
            throw new IllegalArgumentException("UTCTime string too short");
        }
        this.contents = contents;
        if (!this.isDigit(0) || !this.isDigit(1)) {
            throw new IllegalArgumentException("illegal characters in UTCTime string");
        }
    }
    
    public Date getDate() throws ParseException {
        return new SimpleDateFormat("yyMMddHHmmssz", LocaleUtil.EN_Locale).parse(this.getTime());
    }
    
    public Date getAdjustedDate() throws ParseException {
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssz", LocaleUtil.EN_Locale);
        simpleDateFormat.setTimeZone(new SimpleTimeZone(0, "Z"));
        return simpleDateFormat.parse(this.getAdjustedTime());
    }
    
    public String getTime() {
        final String fromByteArray = Strings.fromByteArray(this.contents);
        if (fromByteArray.indexOf(45) < 0 && fromByteArray.indexOf(43) < 0) {
            if (fromByteArray.length() == 11) {
                return fromByteArray.substring(0, 10) + "00GMT+00:00";
            }
            return fromByteArray.substring(0, 12) + "GMT+00:00";
        }
        else {
            int n = fromByteArray.indexOf(45);
            if (n < 0) {
                n = fromByteArray.indexOf(43);
            }
            String string = fromByteArray;
            if (n == fromByteArray.length() - 3) {
                string += "00";
            }
            if (n == 10) {
                return string.substring(0, 10) + "00GMT" + string.substring(10, 13) + ":" + string.substring(13, 15);
            }
            return string.substring(0, 12) + "GMT" + string.substring(12, 15) + ":" + string.substring(15, 17);
        }
    }
    
    public String getAdjustedTime() {
        final String time = this.getTime();
        if (time.charAt(0) < '5') {
            return "20" + time;
        }
        return "19" + time;
    }
    
    private boolean isDigit(final int n) {
        return this.contents.length > n && this.contents[n] >= 48 && this.contents[n] <= 57;
    }
    
    @Override
    final boolean encodeConstructed() {
        return false;
    }
    
    @Override
    int encodedLength(final boolean b) {
        return ASN1OutputStream.getLengthOfEncodingDL(b, this.contents.length);
    }
    
    @Override
    void encode(final ASN1OutputStream asn1OutputStream, final boolean b) throws IOException {
        asn1OutputStream.writeEncodingDL(b, 23, this.contents);
    }
    
    @Override
    boolean asn1Equals(final ASN1Primitive asn1Primitive) {
        return asn1Primitive instanceof ASN1UTCTime && Arrays.areEqual(this.contents, ((ASN1UTCTime)asn1Primitive).contents);
    }
    
    @Override
    public int hashCode() {
        return Arrays.hashCode(this.contents);
    }
    
    @Override
    public String toString() {
        return Strings.fromByteArray(this.contents);
    }
    
    static ASN1UTCTime createPrimitive(final byte[] array) {
        return new ASN1UTCTime(array);
    }
    
    static {
        TYPE = new ASN1UniversalType(23) {
            @Override
            ASN1Primitive fromImplicitPrimitive(final DEROctetString derOctetString) {
                return ASN1UTCTime.createPrimitive(derOctetString.getOctets());
            }
        };
    }
}
