tryParse static method

DateTime? tryParse(
  1. String value
)

Implementation

static DateTime? tryParse(String value) {
  final match = _calendarDateTime.firstMatch(value.trim());
  if (match == null) return null;

  final year = int.parse(match.group(1)!);
  final month = int.parse(match.group(2)!);
  final day = int.parse(match.group(3)!);
  final hour = int.parse(match.group(4) ?? '0');
  final minute = int.parse(match.group(5) ?? '0');
  final second = int.parse(match.group(6) ?? '0');
  final microsecond = _parseFraction(match.group(7));

  final wallClock = DateTime.utc(
    year,
    month,
    day,
    hour,
    minute,
    second,
    microsecond ~/ Duration.microsecondsPerMillisecond,
    microsecond % Duration.microsecondsPerMillisecond,
  );
  if (!_matchesCalendarFields(
    wallClock,
    year: year,
    month: month,
    day: day,
    hour: hour,
    minute: minute,
    second: second,
  )) {
    return null;
  }

  final offset = _parseOffset(match);
  if (offset == null) return null;
  return wallClock.subtract(offset);
}