toDuration method
Implementation
Duration toDuration() {
if (!RegExp(
r"^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$")
.hasMatch(this)) {
throw ArgumentError("String does not follow correct format");
}
final weeks = _parseTime(this, "W");
final days = _parseTime(this, "D");
final hours = _parseTime(this, "H");
final minutes = _parseTime(this, "M");
final seconds = _parseTime(this, "S");
return Duration(
days: days + (weeks * 7),
hours: hours,
minutes: minutes,
seconds: seconds,
);
}