isValidDateParts static method

  1. @useResult
bool isValidDateParts({
  1. int? year,
  2. int? month,
  3. int? day,
  4. int? hour,
  5. int? minute,
  6. int? second,
  7. int? millisecond,
  8. int? microsecond,
})

Returns true if all provided date/time components are within valid ranges.

Valid ranges for each component:

  • day: 1 to max days in month (requires month to be set)

Components that are null are not validated.

Implementation

@useResult
// All 8 named params are needed to validate each DateTime component
// ignore: avoid_long_parameter_list
static bool isValidDateParts({
  int? year,
  int? month,
  int? day,
  int? hour,
  int? minute,
  int? second,
  int? millisecond,
  int? microsecond,
}) {
  if (!_isInRange(value: year, min: 0, max: DateConstants.maxYear)) {
    return false;
  }

  if (!_isInRange(value: month, min: DateConstants.minMonth, max: DateConstants.maxMonth)) {
    return false;
  }

  if (!_isValidDay(day: day, month: month, year: year)) {
    return false;
  }

  if (!_isInRange(value: hour, min: 0, max: DateConstants.maxHour)) {
    return false;
  }

  if (!_isInRange(value: minute, min: 0, max: DateConstants.maxMinuteOrSecond)) {
    return false;
  }

  if (!_isInRange(value: second, min: 0, max: DateConstants.maxMinuteOrSecond)) {
    return false;
  }

  if (!_isInRange(value: millisecond, min: 0, max: DateConstants.maxMillisecondOrMicrosecond)) {
    return false;
  }

  return _isInRange(value: microsecond, min: 0, max: DateConstants.maxMillisecondOrMicrosecond);
}