import Foundation public enum DubaiTime { private static let stampFormatter = LockedDateFormatter(dateFormat: "d MMM yyyy, HH:mm 'Dubai'") private static let fileStampFormatter = LockedDateFormatter(dateFormat: "yyyyMMdd-HHmmss") private static let fileMinuteStampFormatter = LockedDateFormatter(dateFormat: "yyyyMMdd-HHmm") private static let checkTimeFormatter = LockedDateFormatter(dateFormat: "HH:mm 'Dubai'") private static let shortStampFormatter = LockedDateFormatter(dateFormat: "d MMM yyyy, HH:mm") private static let dayStampFormatter = LockedDateFormatter(dateFormat: "d MMM yyyy") private static let keywordStampFormatter = LockedDateFormatter(dateFormat: "yyyy-MM-dd HH:mm 'Dubai'") public static func stamp(_ date: Date) -> String { stampFormatter.string(from: date) } public static func fileStamp(_ date: Date) -> String { fileStampFormatter.string(from: date) } /// `yyyyMMdd-HHmm` — used in PDF file names where minutes are the useful detail. public static func fileMinuteStamp(_ date: Date) -> String { fileMinuteStampFormatter.string(from: date) } /// "31 Oct 2026, 10:31" — compact session-list timestamp (the timezone is stated /// once in the list header, not on every row). public static func shortStamp(_ date: Date) -> String { shortStampFormatter.string(from: date) } /// "31 Oct 2026" — for day-granular expiry notices. public static func dayStamp(_ date: Date) -> String { dayStampFormatter.string(from: date) } /// "2026-10-31 11:00 Dubai" — for PDF keywords, where a comma would be read as a /// keyword separator by the PDF format itself. public static func keywordStamp(_ date: Date) -> String { keywordStampFormatter.string(from: date) } public static func checkTime(_ date: Date) -> String { checkTimeFormatter.string(from: date) } } /// DateFormatter is not Sendable. This holder is the only shared mutable state /// around a formatter: every read is serialized by the lock, so concurrent /// calls (one per PDF page) cannot race. private final class LockedDateFormatter: @unchecked Sendable { private let lock = NSLock() private let formatter: DateFormatter init(dateFormat: String) { let formatter = DateFormatter() formatter.calendar = Calendar(identifier: .gregorian) formatter.locale = Locale(identifier: "en_GB") guard let dubai = TimeZone(identifier: "Asia/Dubai") else { preconditionFailure("The Asia/Dubai time zone is missing from this system.") } formatter.timeZone = dubai formatter.dateFormat = dateFormat self.formatter = formatter } func string(from date: Date) -> String { lock.lock() defer { lock.unlock() } return formatter.string(from: date) } }