CGMSensorDisplayState.swift 1.1 KB

1234567891011121314151617181920212223
  1. import Foundation
  2. /// Compact `5d 14h` / `22h` / `45m` remaining-time formatter. Used as a
  3. /// fallback tag label when the CGM doesn't surface a `cgmStatusHighlight`
  4. /// but the lifecycle progress lets us derive an expiration date.
  5. enum SensorRemainingTimeFormatter {
  6. static func format(until expiresAt: Date, now: Date = Date()) -> String {
  7. let remaining = max(0, expiresAt.timeIntervalSince(now))
  8. let totalMinutes = Int(remaining) / 60
  9. let days = totalMinutes / (60 * 24)
  10. let hours = (totalMinutes / 60) % 24
  11. let minutes = totalMinutes % 60
  12. let d = String(localized: "d", comment: "Abbreviation for Days")
  13. let h = String(localized: "h", comment: "Abbreviation for Hours")
  14. let m = String(localized: "m", comment: "Abbreviation for Minutes")
  15. if days > 0, hours > 0 { return "\(days)\(d) \(hours)\(h)" }
  16. if days > 0 { return "\(days)\(d)" }
  17. if hours > 0 { return "\(hours)\(h)" }
  18. if minutes > 0 { return "\(minutes)\(m)" }
  19. return "<" + "\u{00A0}" + "1" + "\u{00A0}" + m
  20. }
  21. }