parseCompactDate method

  1. @override
DateTime? parseCompactDate(
  1. String? inputString
)
override

Converts the given compact date formatted string into a DateTime.

The format of the string must be a valid compact date format for the given locale. If the text doesn't represent a valid date, null will be returned.

See also:

Implementation

@override
DateTime? parseCompactDate(String? inputString) {
  if (inputString == null) return null;

  // Preserves the legacy dd/mm/yyyy contract of this English localization.
  final List<String> inputParts = inputString.split('/');
  if (inputParts.length != 3) return null;

  final int? year = int.tryParse(inputParts[2].trim(), radix: 10);
  if (year == null) return null;

  final int? month = int.tryParse(inputParts[1].trim(), radix: 10);
  if (month == null || month < 1 || month > 12) return null;

  final int? day = int.tryParse(inputParts[0].trim(), radix: 10);
  if (day == null) return null;

  try {
    if (!HijriDateTime.isValidDate(year, month, day)) return null;
    if (day > _getDaysInMonth(year, month)) return null;
    return HijriDateTime(year, month, day);
  } on RangeError {
    return null;
  } on ArgumentError {
    return null;
  }
}