Skip to content

Commit

Permalink
Use Swift 6.0 ISO8601 format parser/style if available
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Dec 24, 2024
1 parent 6f0130b commit 61991a4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
//
//===----------------------------------------------------------------------===//

#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif

internal enum URLEncodedForm {
/// CodingKey used by URLEncodedFormEncoder and URLEncodedFormDecoder
Expand Down Expand Up @@ -43,10 +47,12 @@ internal enum URLEncodedForm {
fileprivate static let `super` = Key(stringValue: "super")!
}

#if compiler(<6.0)
/// ISO8601 data formatter used throughout URL encoded form code
static var iso8601Formatter: ISO8601DateFormatter {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = .withInternetDateTime
return formatter
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -621,15 +621,17 @@ extension _URLEncodedFormDecoder {
let seconds = try unbox(node, as: Double.self)
return Date(timeIntervalSince1970: seconds)
case .iso8601:
if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
let dateString = try unbox(node, as: String.self)
guard let date = URLEncodedForm.iso8601Formatter.date(from: dateString) else {
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Invalid date format"))
}
return date
} else {
preconditionFailure("ISO8601DateFormatter is unavailable on this platform")
let dateString = try unbox(node, as: String.self)
#if compiler(>=6.0)
guard let date = try? Date(dateString, strategy: .iso8601) else {
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Invalid date format"))
}
#else
guard let date = URLEncodedForm.iso8601Formatter.date(from: dateString) else {
throw DecodingError.dataCorrupted(.init(codingPath: self.codingPath, debugDescription: "Invalid date format"))
}
#endif
return date
case .formatted(let formatter):
let dateString = try unbox(node, as: String.self)
guard let date = formatter.date(from: dateString) else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,11 @@ extension _URLEncodedFormEncoder {
case .secondsSince1970:
try self.encode(Double(date.timeIntervalSince1970).description)
case .iso8601:
if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
try encode(URLEncodedForm.iso8601Formatter.string(from: date))
} else {
preconditionFailure("ISO8601DateFormatter is unavailable on this platform")
}
#if compiler(>=6.0)
try self.encode(date.formatted(.iso8601))
#else
try self.encode(URLEncodedForm.iso8601Formatter.string(from: date))
#endif
case .formatted(let formatter):
try self.encode(formatter.string(from: date))
case .custom(let closure):
Expand Down

0 comments on commit 61991a4

Please sign in to comment.