parseCompactDate method
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:
- formatCompactDate, which will convert a DateTime into a string in the compact format.
Implementation
@override
ChineseDateTime? parseCompactDate(String? inputString) {
if (inputString == null) return null;
// int.tryParse alone also accepts hexadecimal and signed components.
final match =
RegExp(r'^\s*([0-9]{1,2})\s*/\s*([0-9]{1,2})\s*/\s*([0-9]{4})\s*$')
.firstMatch(inputString);
if (match == null || match.end != inputString.length) return null;
final day = int.parse(match[1]!);
final month = int.parse(match[2]!);
final year = int.parse(match[3]!);
if (!ChineseDateTime.isValidDate(year, month, day)) {
return null;
}
return ChineseDateTime(year, month, day);
}