1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if canImport(FoundationEssentials)
import FoundationEssentials
#endif
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension Date {
public struct FormatString : Hashable, Sendable {
internal var rawFormat: String = ""
}
}
extension String {
fileprivate func asDateFormatLiteral() -> String {
guard !self.isEmpty else { return self }
// CLDR uses two adjacent single vertical quotes to represent a literal
// single quote in the template. For the rest of the cases, surround the
// text between single quotes as literal text.
guard self.contains(where: { $0 != "'" }) else {
return String(repeating: "'", count: 2 * count)
}
return "'\(self.replacing("'", with: "''"))'"
}
}
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
extension Date.FormatString : ExpressibleByStringInterpolation {
public init(stringInterpolation: StringInterpolation) {
rawFormat = stringInterpolation.format
}
public init(stringLiteral value: String) {
rawFormat = value.asDateFormatLiteral()
}
public struct StringInterpolation : StringInterpolationProtocol, Sendable {
public typealias StringLiteralType = String
fileprivate var format: String = ""
public init(literalCapacity: Int, interpolationCount: Int) {}
mutating public func appendLiteral(_ literal: String) {
format += literal.asDateFormatLiteral()
}
mutating public func appendInterpolation(era: Date.FormatStyle.Symbol.Era) {
guard let option = era.option else {
return
}
format.append(option.rawValue)
}
mutating public func appendInterpolation(year: Date.FormatStyle.Symbol.Year) {
guard let option = year.option else {
return
}
format.append(option.rawValue)
}
mutating public func appendInterpolation(yearForWeekOfYear: Date.FormatStyle.Symbol.YearForWeekOfYear) {
guard let option = yearForWeekOfYear.option else {
return
}
format.append(option.rawValue)
}
mutating public func appendInterpolation(cyclicYear: Date.FormatStyle.Symbol.CyclicYear) {
guard let option = cyclicYear.option else {
return
}
format.append(option.rawValue)
}
mutating public func appendInterpolation(quarter: Date.FormatStyle.Symbol.Quarter) {
guard let option = quarter.option else {
return
}
format.append(option.rawValue)
}
mutating public func appendInterpolation(standaloneQuarter: Date.FormatStyle.Symbol.StandaloneQuarter) {
format.append(standaloneQuarter.option.rawValue)
}
mutating public func appendInterpolation(month: Date.FormatStyle.Symbol.Month) {
guard let option = month.option else {
return
}
format.append(option.rawValue)
}
mutating public func appendInterpolation(standaloneMonth: Date.FormatStyle.Symbol.StandaloneMonth) {
format.append(standaloneMonth.option.rawValue)
}
mutating public func appendInterpolation(week: Date.FormatStyle.Symbol.Week) {
guard let option = week.option else {
return
}
format.append(option.rawValue)
}
mutating public func appendInterpolation(day: Date.FormatStyle.Symbol.Day) {
guard let option = day.option else {
return
}
format.append(option.rawValue)
}
mutating public func appendInterpolation(dayOfYear: Date.FormatStyle.Symbol.DayOfYear) {
guard let option = dayOfYear.option else {
return
}
format.append(option.rawValue)
}
mutating public func appendInterpolation(weekday: Date.FormatStyle.Symbol.Weekday) {
guard let option = weekday.option else {
return
}
format.append(option.rawValue)
}
mutating public func appendInterpolation(standaloneWeekday: Date.FormatStyle.Symbol.StandaloneWeekday) {
format.append(standaloneWeekday.option.rawValue)
}
mutating public func appendInterpolation(dayPeriod: Date.FormatStyle.Symbol.DayPeriod) {
guard let option = dayPeriod.option else {
return
}
format.append(option.rawValue)
}
mutating public func appendInterpolation(hour: Date.FormatStyle.Symbol.VerbatimHour) {
format.append(hour.option.rawValue)
}
mutating public func appendInterpolation(minute: Date.FormatStyle.Symbol.Minute) {
guard let option = minute.option else {
return
}
format.append(option.rawValue)
}
mutating public func appendInterpolation(second: Date.FormatStyle.Symbol.Second) {
guard let option = second.option else {
return
}
format.append(option.rawValue)
}
mutating public func appendInterpolation(secondFraction: Date.FormatStyle.Symbol.SecondFraction) {
guard let option = secondFraction.option else {
return
}
format.append(option.rawValue)
}
mutating public func appendInterpolation(timeZone: Date.FormatStyle.Symbol.TimeZone) {
guard let option = timeZone.option else {
return
}
format.append(option.rawValue)
}
}
}
|