generalizedTimeToDate static method
Converts a GeneralizedTime value to a date.
contentData
the GeneralizedTime value to convert.
Implementation
static DateTime? generalizedTimeToDate({required List<int> contentData}) {
/* The following formats can be used:
YYYYMMDDHHMMSS
YYYYMMDDHHMMSS.fff
YYYYMMDDHHMMSSZ
YYYYMMDDHHMMSS.fffZ
YYYYMMDDHHMMSS+hh'mm'
YYYYMMDDHHMMSS.fff+hh'mm'
YYYYMMDDHHMMSS-hh'mm'
YYYYMMDDHHMMSS.fff-hh'mm'
Where:
YYYY is 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)
.fff is the second fraction, accurate to three decimal places
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? gentime;
try {
gentime = utf8.decode(contentData);
} catch (e) {}
if (gentime == null) {
return null;
}
// if YY >= 50 use 19xx, if YY < 50 use 20xx
// ignore: non_constant_identifier_names
var YYYY = int.parse(gentime.substring(0, 4), radix: 10);
// ignore: non_constant_identifier_names
var MM = int.parse(gentime.substring(4, 6), radix: 10);
// ignore: non_constant_identifier_names
var DD = int.parse(gentime.substring(6, 8), radix: 10);
var hh = int.parse(gentime.substring(8, 10), radix: 10);
var mm = int.parse(gentime.substring(10, 12), radix: 10);
var ss = int.parse(gentime.substring(12, 14), radix: 10);
double fff = 0.0;
var offset = 0;
var isUTC = false;
if (gentime[gentime.length - 1] == 'Z') {
isUTC = true;
}
var end = gentime.length - 5;
var c = gentime[end];
if (c == '+' || c == '-') {
// get hours+minutes offset
var hhoffset =
int.parse(gentime.substring(end + 1, end + 1 + 2), radix: 10);
var mmoffset =
int.parse(gentime.substring(end + 4, end + 4 + 2), radix: 10);
// calculate offset in milliseconds
offset = hhoffset * 60 + mmoffset;
offset *= 60000;
// apply offset
if (c == '+') {
offset *= -1;
}
isUTC = true;
}
// check for second fraction
if (gentime[14] == '.') {
fff = double.parse(gentime.substring(14)) * 1000;
}
var date = DateTime.utc(YYYY, MM, DD, hh, mm, ss, fff.toInt());
if (isUTC) {
var offsetDuration = Duration(milliseconds: offset);
date.add(offsetDuration);
}
return date;
}