fromText method
text to duration example:
days - hours - minutes - seconds - milliseconds - microseconds
0 - 0 - 0 - 0 - 0 - 0
Duration().fromText("0-0-0-0-0-0");;
Implementation
Duration fromText(String text) {
DurationDataScheme durationData = DurationDataScheme({
"@type": "durationDataScheme",
});
RegExpMatch? regExpMatch = RegExp(
r"(([0-9]+)[ \-\~+]+([0-9]+)[ \-\~+]+([0-9]+)[ \-\~+]+([0-9]+)[ \-\~+]+([0-9]+)[ \-\~+]+([0-9]+))",
caseSensitive: false)
.firstMatch(text);
if (regExpMatch != null) {
List<String?> res = regExpMatch.groups([2, 3, 4, 5, 6, 7]);
List<String> keys = [
"days",
"hours",
"minutes",
"seconds",
"milliseconds",
"microseconds",
];
for (var i = 0; i < res.length; i++) {
String? datas = res[i];
if (datas == null) {
continue;
}
try {
durationData[keys[i]] = int.parse(datas);
} catch (e) {}
}
}
return Duration(
days: durationData.days ?? 0,
hours: durationData.hours ?? 0,
minutes: durationData.minutes ?? 0,
seconds: durationData.seconds ?? 0,
milliseconds: durationData.milliseconds ?? 0,
microseconds: durationData.microseconds ?? 0,
);
}