toSecondsInt static method

int toSecondsInt(
  1. String _value
)

Implementation

static int toSecondsInt(String _value) {
  String value = _value;

  if (value.length == 5) {
    value = '00:$value';
  }

  final regex = RegExp(r'^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$');

  final match = regex.allMatches(value);

  if (match.isEmpty) {
    return int.tryParse(value) ?? 0;
  }

  final hours = int.tryParse(match.elementAt(0).group(1) ?? '') ?? 0;
  final minutes = int.tryParse(match.elementAt(0).group(2) ?? '') ?? 0;
  final seconds = int.tryParse(match.elementAt(0).group(3) ?? '') ?? 0;

  return hours * 3600 + minutes * 60 + seconds;
}