|
| 1 | +// |
| 2 | +// CalendarDependentFormatStyle.swift |
| 3 | +// OpenSwiftUICore |
| 4 | +// |
| 5 | +// Audited for 6.5.4 |
| 6 | +// Status: Complete (Blocked by WhitespaceRemovingFormatStyle/SystemFormatStyle) |
| 7 | +// ID: 26D279F2E8972E56094553A13FA39915 (SwiftUICore) |
| 8 | + |
| 9 | +package import Foundation |
| 10 | + |
| 11 | +protocol CalendarDependentFormatStyle: FormatStyle { |
| 12 | + func withCalendar(_ calendar: Calendar) -> Self |
| 13 | +} |
| 14 | + |
| 15 | +extension FormatStyle { |
| 16 | + package func calendar(_ calendar: Calendar) -> Self { |
| 17 | + guard let style = self as? any CalendarDependentFormatStyle else { |
| 18 | + return self |
| 19 | + } |
| 20 | + return style.withCalendar(calendar) as! Self |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +#if canImport(Darwin) |
| 25 | +@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) |
| 26 | +extension Date.FormatStyle: CalendarDependentFormatStyle { |
| 27 | + func withCalendar(_ calendar: Calendar) -> Self { |
| 28 | + var style = self |
| 29 | + style.calendar = calendar |
| 30 | + return style |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) |
| 35 | +extension Date.VerbatimFormatStyle: CalendarDependentFormatStyle { |
| 36 | + func withCalendar(_ calendar: Calendar) -> Self { |
| 37 | + var style = self |
| 38 | + style.calendar = calendar |
| 39 | + return style |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) |
| 44 | +extension Date.ComponentsFormatStyle: CalendarDependentFormatStyle { |
| 45 | + func withCalendar(_ calendar: Calendar) -> Self { |
| 46 | + self.calendar(calendar) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *) |
| 51 | +extension Date.FormatStyle.Attributed: CalendarDependentFormatStyle { |
| 52 | + func withCalendar(_ calendar: Calendar) -> Self { |
| 53 | + var style = self |
| 54 | + style[dynamicMember: \.calendar] = calendar |
| 55 | + return style |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +@available(macOS 15, iOS 18, tvOS 18, watchOS 11, *) |
| 60 | +extension Date.VerbatimFormatStyle.Attributed: CalendarDependentFormatStyle { |
| 61 | + func withCalendar(_ calendar: Calendar) -> Self { |
| 62 | + var style = self |
| 63 | + style[dynamicMember: \.calendar] = calendar |
| 64 | + return style |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) |
| 69 | +extension Date.AnchoredRelativeFormatStyle: CalendarDependentFormatStyle { |
| 70 | + func withCalendar(_ calendar: Calendar) -> Self { |
| 71 | + var style = self |
| 72 | + style.calendar = calendar |
| 73 | + return style |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// TODO: Add conformance when these concrete format styles land: |
| 78 | +// WhitespaceRemovingFormatStyle where A: CalendarDependentFormatStyle |
| 79 | +// SystemFormatStyle.DateReference |
| 80 | +#endif |
0 commit comments