dateFromString static method

DateTime dateFromString(
  1. String str
)

NOTE: Effectively, any date is always converted to the corresponding ms-precise date-time at midnight of that date. Note that that doesn't properly reflect the resolution of the input date. That effect has to be taken into account by the logic implementor.

Implementation

static DateTime dateFromString(String str) {
  DateTime? dateTime;
  if (str.length == 10) {
    // No TZ is added
    final date = DateTime.parse(str);
    dateTime = DateTime.utc(
        date.year, date.month, date.day); // Set it to midnight as per specs
  }
  final regex =
      r'(^[T,\-,:,\.0-9]{10,}[+,-])([0-9]|[0-9]{3}|[0-9]:[0-9]{2})$';
  str = str.replaceAllMapped(
      RegExp(regex), (match) => '${match.group(1)}0${match.group(2)}');
  dateTime ??= DateTime.parse(str);
  return DateTime.utc(dateTime.year, dateTime.month, dateTime.day,
      dateTime.hour, dateTime.minute, dateTime.second, dateTime.millisecond);
}