TimeUnitDuration.fromString constructor
TimeUnitDuration.fromString(
- String str
Implementation
TimeUnitDuration.fromString(String str) {
String tudString = str.toString().trim();
List<dynamic> matches = [
...RegExp(r'\d+|[A-Za-z]+')
.allMatches(tudString)
.map((match) => match[0]!)
.map((string) => int.tryParse(string) ?? string)
];
int? amt;
String? time;
if (matches.length >= 2 &&
matches[0].runtimeType == int &&
matches[1].runtimeType == String) {
amt = matches[0]!;
time = matches[1]!;
}
if (amt == null || time == null) {
amount = 0;
timeUnit = TimeUnit.millisecond;
} else {
switch (time.trim().toLowerCase()) {
case 'ms':
case 'millisecond':
case 'milliseconds':
amount = amt;
timeUnit = TimeUnit.millisecond;
break;
case 's':
case 'sec':
case 'second':
case 'seconds':
amount = amt;
timeUnit = TimeUnit.second;
break;
case 'm':
case 'min':
case 'minute':
case 'minutes':
amount = amt;
timeUnit = TimeUnit.minute;
break;
case 'h':
case 'hr':
case 'hour':
case 'hours':
amount = amt;
timeUnit = TimeUnit.hour;
break;
case 'd':
case 'day':
case 'days':
amount = amt;
timeUnit = TimeUnit.day;
break;
case 'w':
case 'week':
case 'weeks':
amount = amt;
timeUnit = TimeUnit.week;
break;
case 'mo':
case 'month':
case 'months':
amount = amt;
timeUnit = TimeUnit.month;
break;
case 'y':
case 'yr':
case 'year':
case 'years':
amount = amt;
timeUnit = TimeUnit.year;
break;
default:
amount = 0;
timeUnit = TimeUnit.millisecond;
break;
}
}
}