incrementTime function

TimeFlipEvent incrementTime(
  1. TimeFlipEvent time,
  2. bool is12
)

Increments the given TimeFlipEvent by one second, handling rollover. is12 indicates whether the time is in 12-hour format.

Implementation

TimeFlipEvent incrementTime(TimeFlipEvent time, bool is12) {
  int h = time.hour;
  int m = time.minute;
  int s = time.second;
  s++;
  if (s >= 60) {
    s = 0;
    m++;
  }
  if (m >= 60) {
    m = 0;
    h++;
  }
  if (!is12 && h >= 24) {
    h = 0;
  } else if (is12 && h > 12) {
    // For 12-hour format, if hour exceeds 12, convert and toggle period.
    // We'll use a simple approach: if h becomes 13, then set it to 1 and toggle period.
    h = 1;
    String newPeriod = time.period == "AM" ? "PM" : "AM";
    return TimeFlipEvent(hour: h, minute: m, second: s, period: newPeriod);
  }
  if (is12) {
    // In 12-hour mode, determine period based on the underlying 24-hour logic.
    // Since our input is in 12-hour format, we assume period toggling is handled on rollover.
    return TimeFlipEvent(hour: h, minute: m, second: s, period: time.period);
  }
  return TimeFlipEvent(hour: h, minute: m, second: s, period: null);
}