parse static method
Parses a Jalali date such as 1403/01/01, 1403-1-1, or
۱۴۰۳/۰۱/۰۱ ۱۴:۳۰.
Accepts / or - between year, month, and day (the same one twice),
an optional time of
HH:mm or HH:mm:ss after a space or T, and ASCII, Persian, or
Arabic-Indic digits. With a time the result is a local
PersianDate.withTime; without one it is date-only like
PersianDate.new.
The year is read in era, so with JalaliEra.shahanshahi 2585/01/01
is Solar Hijri 1405/01/01.
Throws FormatException when input does not match or is not a valid
date, like DateTime.parse.
Implementation
static PersianDate parse(String input,
{JalaliEra era = JalaliEra.solarHijri}) {
final match = _parsePattern.firstMatch(toLatinDigits(input));
if (match == null) {
throw FormatException('Invalid Jalali date', input);
}
int part(int group) => int.parse(match.group(group)!);
final firstYear = -61 + era.offset;
final lastYear = 3177 + era.offset;
if (part(1) < firstYear || part(1) > lastYear) {
throw FormatException(
'Invalid Jalali date: year must be in $firstYear to $lastYear',
input,
);
}
final year = part(1) - era.offset;
try {
if (match.group(5) == null) {
return PersianDate(year, part(3), part(4));
}
return PersianDate.withTime(
year,
part(3),
part(4),
part(5),
part(6),
match.group(7) == null ? 0 : part(7),
);
} on ArgumentError catch (error) {
throw FormatException('Invalid Jalali date: ${error.message}', input);
}
}