// Their clock: the time in another place's zone, said only when it is not // this phone's own. One rule for every face that reads a zone — a slip's // trailing clock and a page's "Their time" line (a column marked `zone`) — so // neither can drift from the other. Moved here from // the old People screens, which drew it first. // // EVERY function takes its `now`; nothing here reads the wall clock. import CottonKit import Foundation public enum ZoneClock { /// Their clock, in the app's ONE spelling — "7:30 pm", lowercase am/pm. /// /// Not `DateFormats.houseTime` itself, because that formats in the calendar /// it is handed and this has to place an instant in ANOTHER zone: the /// pattern is the same and the zone is the difference. `nil` when the /// runtime cannot place the zone — a row then says nothing. public static func zoneTime(_ at: Date, zone: String) -> String? { guard let timeZone = TimeZone(identifier: zone) else { return nil } var calendar = Calendar(identifier: .gregorian) calendar.timeZone = timeZone return DateFormats.houseTime(at, calendar: calendar) } /// Whether their clock is worth stating at `at` — THE one rule, which /// pages and slips both ask. /// /// Only when their zone's UTC offset AT THAT INSTANT differs from this /// device's. Offsets, not identifiers: Asia/Calcutta and Asia/Kolkata are /// one clock under two names, and so are Europe/Paris and Europe/Berlin — /// either would otherwise be your own clock wearing their name. And at the /// instant, not in general: London and Lisbon agree all year, but London /// and New York are five hours apart for most of it and four for the weeks /// their daylight-saving changes fall on different Sundays. No zone is no /// clock. public static func showsTheirTime(_ zone: TimeZone?, at: Date, deviceZone: TimeZone = .current) -> Bool { guard let zone else { return false } return zone.secondsFromGMT(for: at) != deviceZone.secondsFromGMT(for: at) } /// A slip's clock: "7:30 pm", or nothing when they keep this device's /// clock (`showsTheirTime`) or the zone cannot be placed. public static func short(zone: String?, now: Date, deviceZone: TimeZone = .current) -> String? { guard let zone = zone?.trimmingCharacters(in: .whitespacesAndNewlines), !zone.isEmpty, showsTheirTime(TimeZone(identifier: zone), at: now, deviceZone: deviceZone) else { return nil } return zoneTime(now, zone: zone) } }