verify method
Verify that we correspond to a valid date. This will reject out of range values, even if the DateTime constructor would accept them. An invalid message will result in throwing a FormatException.
Implementation
void verify(String s) {
_verify(month, 1, 12, 'month', s);
_verify(hour24, 0, 23, 'hour', s);
_verify(minute, 0, 59, 'minute', s);
_verify(second, 0, 59, 'second', s);
_verify(fractionalSecond, 0, 999, 'fractional second', s);
// Verifying the day is tricky, because it depends on the month. Create
// our resulting date and then verify that our values agree with it
// as an additional verification. And since we're doing that, also
// check the year, which we otherwise can't verify, and the hours,
// which will catch cases like '14:00:00 PM'.
var date = asDate();
// On rare occasions, possibly related to DST boundaries, a parsed date will
// come out as 1:00am. We compensate for the case of going backwards in
// _correctForErrors, but we may not be able to compensate for a midnight
// that doesn't exist. So tolerate an hour value of zero or one in these
// cases.
var minimumDate = dateOnly && date.hour == 1 ? 0 : date.hour;
_verify(hour24, minimumDate, date.hour, 'hour', s, date);
if (dayOfYear > 0) {
// We have an ordinal date, compute the corresponding date for the result
// and compare to that.
var leapYear = date_computation.isLeapYear(date);
var correspondingDay =
date_computation.dayOfYear(date.month, date.day, leapYear);
_verify(
dayOfYear, correspondingDay, correspondingDay, 'dayOfYear', s, date);
} else {
// We have the day of the month, compare directly.
_verify(day, date.day, date.day, 'day', s, date);
}
_verify(year, date.year, date.year, 'year', s, date);
}