tryParse static method
Attempts to parse a string representation of a duration.
Implementation
static XPathDuration? tryParse(String value, [XPathType type = xsDuration]) {
if (type == xsYearMonthDuration) {
return tryParseYearMonth(value);
}
if (type == xsDayTimeDuration) {
return tryParseDayTime(value);
}
final match = _durationRegExp.firstMatch(value);
if (match == null) return null;
final hasYM = match.group(2) != null || match.group(3) != null;
final hasDT =
match.group(4) != null ||
match.group(5) != null ||
match.group(6) != null ||
match.group(7) != null;
if (!hasYM && !hasDT) return null;
final negative = match.group(1) == '-';
final yr = int.tryParse(match.group(2) ?? '0') ?? 0;
final mo = int.tryParse(match.group(3) ?? '0') ?? 0;
final dy = int.tryParse(match.group(4) ?? '0') ?? 0;
final hr = int.tryParse(match.group(5) ?? '0') ?? 0;
final min = int.tryParse(match.group(6) ?? '0') ?? 0;
final scDouble = double.tryParse(match.group(7) ?? '0') ?? 0.0;
final totalSeconds = scDouble.truncate();
final frac = scDouble - totalSeconds;
final ms = (frac * 1000).truncate();
final us = ((frac * 1000000) - (ms * 1000)).round();
final sec = totalSeconds % 60;
final totalMin = min + totalSeconds ~/ 60;
final m = totalMin % 60;
final totalHr = hr + totalMin ~/ 60;
final h = totalHr % 24;
final d = dy + totalHr ~/ 24;
return XPathDuration(
years: yr,
months: mo,
days: d,
hours: h,
minutes: m,
seconds: sec,
milliseconds: ms,
microseconds: us,
isNegative: negative,
type: type,
);
}