struct CompactCountdownFormatStyle: DiscreteFormatStyle { func discreteInput(before input: Duration) -> Duration? { // This should be the upper bound of the countdown, in other words, what's the highest start time // we support. // If our input it already a minute or less then that's our cap guard input > .seconds(60) else { return nil } // Since our boundary is minutes, what the next minute smaller than our input? let minutes = Int(input.components.seconds) / 60 if minutes <= 1 { return nil } else { return .seconds((minutes - 1) * 60) } } func discreteInput(after input: Duration) -> Duration? { if input > .seconds(60 * 60 * 8) { return nil } else { // One minute over the given duration? return input + .seconds(60) } } func format(_ value: Duration) -> String { let hoursRemaining = Int(value.components.seconds) / 60 / 60 let minutesRemaining = Int(value.components.seconds) / 60 % 60 if hoursRemaining > 0 { return "\(hoursRemaining):\(minutesRemaining.formatted(.number.precision(.integerLength(2))))h" } else { return "\(minutesRemaining)m" } } }