durationToTrigger static method

String durationToTrigger(
  1. Duration duration
)

Implementation

static String durationToTrigger(Duration duration) {
  // Check if the duration is negative
  bool isNegative = duration.isNegative;

  // Get the absolute values of the duration components
  int totalDays = duration.inDays.abs();
  int totalHours = duration.inHours.abs() % 24;
  int totalMinutes = duration.inMinutes.abs() % 60;
  int totalSeconds = duration.inSeconds.abs() % 60;

  // Create the trigger string parts
  StringBuffer buffer = StringBuffer();

  // Prefix with '-' if the duration is negative
  if (isNegative) {
    buffer.write('-');
  }

  buffer.write('P');

  // Add the day part if greater than zero
  if (totalDays > 0) {
    buffer.write('${totalDays}D');
  }

  // If there are hours, minutes, or seconds, add the time part
  if (totalHours > 0 || totalMinutes > 0 || totalSeconds > 0) {
    buffer.write('T');

    if (totalHours > 0) {
      buffer.write('${totalHours}H');
    }

    if (totalMinutes > 0) {
      buffer.write('${totalMinutes}M');
    }

    if (totalSeconds > 0) {
      buffer.write('${totalSeconds}S');
    }
  }

  return buffer.toString();
}