LocalDate.parse constructor

LocalDate.parse(
  1. String text
)

Parses YYYY-MM-DD, optionally followed by BC.

Implementation

factory LocalDate.parse(String text) {
  final match = _syntax.firstMatch(text);
  if (match == null || match.end != text.length) {
    throw const FormatException(
      'Expected a Gregorian date without a timezone.',
    );
  }
  var year = int.parse(match[1]!);
  if (match[4] != null) {
    if (year <= 0) {
      throw const FormatException('BC dates require a positive era year.');
    }
    year = 1 - year;
  }
  return LocalDate(year, int.parse(match[2]!), int.parse(match[3]!));
}