triggerToDuration static method

Duration triggerToDuration(
  1. String trigger
)

Implementation

static Duration triggerToDuration(String trigger) {
    // Check if the trigger is negative
    bool isNegative = trigger.startsWith('-');

    // Remove the negative sign if present
    String normalizedTrigger = isNegative ? trigger.substring(1) : trigger;

    // Regular expression to match the trigger components
    RegExp regExp = RegExp(r'P((\d+)D)?(T((\d+)H)?((\d+)M)?((\d+)S)?)?');
    Match? match = regExp.firstMatch(normalizedTrigger);

    if (match != null) {
      int days = int.tryParse(match.group(2) ?? '0') ?? 0;
      int hours = int.tryParse(match.group(5) ?? '0') ?? 0;
      int minutes = int.tryParse(match.group(7) ?? '0') ?? 0;
      int seconds = int.tryParse(match.group(9) ?? '0') ?? 0;

      // Create the Duration object
      Duration duration = Duration(
        days: days,
        hours: hours,
        minutes: minutes,
        seconds: seconds,
      );

      // If the trigger was negative, return a negative duration
      return isNegative ? -duration : duration;
    } else {
      throw 'Invalid trigger format: $trigger';
    }
  }