fromString static method
Преобразовать String в Duration
- '1:10:00.000500' -> Duration(hours: 1, minutes: 10, microseconds: 500)
Implementation
static Duration fromString(String str) {
if (str == 'null') str = '';
RegExpMatch? match = RegExp(r'^(-?)(\d+):([0-5]\d):([0-5]\d)\.(\d{6})$').firstMatch(str);
match ??= RegExp(r'^(-?)(\d+):([0-5]\d)$').firstMatch(str);
if (str.isEmpty) return const Duration();
if (match == null) throw const FormatException('Invalid duration format');
final isNegative = match.groupCount >= 1 ? match.group(1) == '-' : false;
final hours = match.groupCount >= 2 ? int.parse(match.group(2)!) : 0;
final minutes = match.groupCount >= 3 ? int.parse(match.group(3)!) : 0;
final seconds = match.groupCount >= 4 ? int.parse(match.group(4)!) : 0;
final microseconds = match.groupCount >= 5 ? int.parse(match.group(5)!) : 0;
final duration = Duration(
hours: hours,
minutes: minutes,
seconds: seconds,
microseconds: microseconds,
);
return isNegative ? -duration : duration;
}