ExtendedDuration.fromString constructor

ExtendedDuration.fromString(
  1. String value
)

Implementation

factory ExtendedDuration.fromString(String value) {
  int years = 0;
  int months = 0;
  int days = 0;
  int hours = 0;
  int minutes = 0;
  int seconds = 0;
  int milliseconds = 0;

  final RegExpMatch? yearMatch = yearRegex.firstMatch(value);
  if (yearMatch != null) {
    years = int.parse(yearMatch.group(1)!);
  }

  final RegExpMatch? monthMatch = monthRegex.firstMatch(value);
  if (monthMatch != null) {
    months = int.parse(monthMatch.group(1)!);
  }

  final RegExpMatch? weekMatch = weekRegex.firstMatch(value);
  if (weekMatch != null) {
    days += int.parse(weekMatch.group(1)!) * 7;
  }

  final RegExpMatch? dayMatch = dayRegex.firstMatch(value);
  if (dayMatch != null) {
    days += int.parse(dayMatch.group(1)!);
  }

  final RegExpMatch? hourMatch = hourRegex.firstMatch(value);
  if (hourMatch != null) {
    hours += int.parse(hourMatch.group(1)!);
  }

  final RegExpMatch? minuteMatch = minuteRegex.firstMatch(value);
  if (minuteMatch != null) {
    minutes += int.parse(minuteMatch.group(1)!);
  }

  final RegExpMatch? secondMatch = secondRegex.firstMatch(value);
  if (secondMatch != null) {
    seconds += int.parse(secondMatch.group(1)!);
  }

  final RegExpMatch? millisecondMatch = millisecondRegex.firstMatch(value);
  if (millisecondMatch != null) {
    milliseconds += int.parse(millisecondMatch.group(1)!);
  }

  return ExtendedDuration(
    years: years,
    months: months,
    days: days,
    hours: hours,
    minutes: minutes,
    seconds: seconds,
    milliseconds: milliseconds,
  );
}