public static DecodeGeneralizedTime ( IBerInput input, int length ) : System.DateTime | ||
input | IBerInput | |
length | int | |
return | System.DateTime |
public static DateTime DecodeGeneralizedTime(IBerInput input, int length)
{
var str = DecodeAsciiString(input, length);
int year, month, day, hour, minute, second, millisecond;
var isUtc = false;
try
{
year = int.Parse(str.Substring(0, 4));
month = int.Parse(str.Substring(4, 2));
day = int.Parse(str.Substring(6, 2));
hour = int.Parse(str.Substring(8, 2));
minute = int.Parse(str.Substring(10, 2));
second = int.Parse(str.Substring(12, 2));
var suffix = str.Substring(15);
if(suffix.EndsWith("Z")) // UTC
{
suffix = suffix.Substring(0, suffix.Length - 1);
isUtc = true;
}
millisecond = int.Parse(suffix);
return new DateTime(year, month, day, hour, minute, second, millisecond, isUtc ? DateTimeKind.Utc : DateTimeKind.Local);
}
catch(Exception ex)
{
Debug.WriteLine(ex);
return DateTime.MinValue;
}
}
/// <summary> /// Decodes the value of the current TLV as an GeneralizedTime represented /// as a DateTime structure. /// Throws an exception in case of a format mismatch. /// </summary> /// <returns>DateTime value containing the GeneralizedTime value of the current TLV.</returns> public DateTime GetGeneralizedTime() { if (IsContainer || Length == 0 || Value == null) { ThrowError(208, "Invalid GeneralizedTime encoding"); } Debug.Assert(Value != null || Length == 0); Debug.Assert(Type == BerType.GeneralizedTime || BerType.IsApplicationDefined(Type)); var input = new BerMemoryInput(Value); return(BerEncoding.DecodeGeneralizedTime(input, Length)); }