utcTimeToDate static method
Converts a UTCTime value to a date.
Note: GeneralizedTime has 4 digits for the year and is used for X.509 dates past 2049. Parsing that structure hasn't been implemented yet.
contentData
the UTCTime value to convert.
Implementation
static DateTime? utcTimeToDate({required List<int> contentData}) {
/* The following formats can be used:
YYMMDDhhmmZ
YYMMDDhhmm+hh'mm'
YYMMDDhhmm-hh'mm'
YYMMDDhhmmssZ
YYMMDDhhmmss+hh'mm'
YYMMDDhhmmss-hh'mm'
Where:
YY is the least significant two digits of the year
MM is the month (01 to 12)
DD is the day (01 to 31)
hh is the hour (00 to 23)
mm are the minutes (00 to 59)
ss are the seconds (00 to 59)
Z indicates that local time is GMT, + indicates that local time is
later than GMT, and - indicates that local time is earlier than GMT
hh' is the absolute value of the offset from GMT in hours
mm' is the absolute value of the offset from GMT in minutes */
String? utc;
try {
utc = utf8.decode(contentData);
} catch (e) {}
if (utc == null) {
return null;
}
// if YY >= 50 use 19xx, if YY < 50 use 20xx
var year = int.parse(utc.substring(0, 2), radix: 10);
year = (year >= 50) ? 1900 + year : 2000 + year;
// ignore: non_constant_identifier_names
var MM = int.parse(utc.substring(2, 4), radix: 10);
// ignore: non_constant_identifier_names
var DD = int.parse(utc.substring(4, 6), radix: 10);
var hh = int.parse(utc.substring(6, 8), radix: 10);
var mm = int.parse(utc.substring(8, 10), radix: 10);
var ss = 0;
int? end;
String? c;
// not just YYMMDDhhmmZ
if (utc.length > 11) {
// get character after minutes
c = utc[10];
end = 10;
// see if seconds are present
if (c != '+' && c != '-') {
// get seconds
ss = int.parse(utc.substring(10, 12), radix: 10);
end += 2;
}
}
var date = DateTime.utc(year, MM, DD, hh, mm, ss, 0);
if (end != null) {
// get +/- after end of time
c = utc[end];
if (c == '+' || c == '-') {
// get hours+minutes offset
var hhoffset =
int.parse(utc.substring(end + 1, end + 1 + 2), radix: 10);
var mmoffset =
int.parse(utc.substring(end + 4, end + 4 + 2), radix: 10);
// calculate offset in milliseconds
var offset = hhoffset * 60 + mmoffset;
offset *= 60000;
var offsetDuration = Duration(milliseconds: offset);
// apply offset
if (c == '+') {
date.subtract(offsetDuration);
} else {
date.add(offsetDuration);
}
}
}
return date;
}