parseDuration function

Duration? parseDuration(
  1. String? unit, [
  2. int amount = 0,
  3. Duration? def
])

Parses unit and amount to Duration.

Implementation

Duration? parseDuration(String? unit, [int amount = 0, Duration? def]) {
  if (unit == null) return def;
  unit = unit.toLowerCase().trim();
  if (unit.isEmpty) return def;

  switch (unit) {
    case 'year':
    case 'years':
      return Duration(days: amount * 365);

    case 'quarter':
    case 'quarters':
      return Duration(days: amount * 90);

    case 'month':
    case 'months':
      return Duration(days: amount * 30);

    case 'd':
    case 'day':
    case 'days':
      return Duration(days: amount);

    case 'h':
    case 'hr':
    case 'hrs':
    case 'hour':
    case 'hours':
      return Duration(hours: amount);

    case 'min':
    case 'minute':
    case 'minutes':
      return Duration(minutes: amount);

    case 's':
    case 'sec':
    case 'second':
    case 'seconds':
      return Duration(seconds: amount);

    case 'ms':
    case 'millis':
    case 'millisecond':
    case 'milliseconds':
      return Duration(milliseconds: amount);

    case 'µs':
    case 'µsec':
    case 'us':
    case 'usec':
    case 'microsecond':
    case 'microseconds':
      return Duration(microseconds: amount);

    default:
      return def;
  }
}