HDate.fromString constructor

HDate.fromString(
  1. String s
)

Parse from string fomat "YYYY-MM-DD" or raise ParseError

Implementation

factory HDate.fromString(String s) {
  ParseError err = ParseError(s);
  if (s.length != "YYYY-MM-DD".length) throw err;
  int year, month, day;

  try {
    year = int.parse(s.substring(0, 4));
  } catch (e) {
    throw ParseError("Invalid year: $s");
  }
  if (s.codeUnitAt(4) != MINUS_CHAR) throw err;

  try {
    month = int.parse(s.substring(5, 7));
  } catch (e) {
    throw ParseError("Invalid month: $s");
  }
  if (s.codeUnitAt(7) != MINUS_CHAR) throw err;
  try {
    day = int.parse(s.substring(8));
  } catch (e) {
    throw ParseError("Invalid day: $s");
  }
  return HDate(year, month, day);
}