parseDuration static method
Implementation
static int? parseDuration(String? time) {
if (time == null) return null;
// Extract only the time portion using regex (format: H:MM:SS or MM:SS or M:SS)
final timeMatch = RegExp(r'(\d+):(\d+)(?::(\d+))?').firstMatch(time);
if (timeMatch == null) return null;
// Parse the matched groups
final parts = <int>[];
for (int i = 1; i <= timeMatch.groupCount; i++) {
final group = timeMatch.group(i);
if (group != null) {
parts.add(int.parse(group));
}
}
if (parts.isEmpty) return null;
// Handle different time formats
if (parts.length == 2) {
// MM:SS format
final minutes = parts[0];
final seconds = parts[1];
return seconds + minutes * 60;
} else if (parts.length == 3) {
// H:MM:SS format
final hours = parts[0];
final minutes = parts[1];
final seconds = parts[2];
return seconds + minutes * 60 + hours * 60 * 60;
}
return null;
}