1414
1515import struct Foundation. Date
1616import class Foundation. DateFormatter
17- import class Foundation. ISO8601DateFormatter
1817import struct Foundation. Locale
18+ import struct Foundation. TimeZone
1919
2020@propertyWrapper
2121public struct ISO8601Coding : Decodable {
@@ -28,28 +28,22 @@ public struct ISO8601Coding: Decodable {
2828 public init ( from decoder: Decoder ) throws {
2929 let container = try decoder. singleValueContainer ( )
3030 let dateString = try container. decode ( String . self)
31- guard let date = Self . decodeDate ( from: dateString) else {
31+ guard let date = Self . dateFormatter . date ( from: dateString) else {
3232 throw DecodingError . dataCorruptedError ( in: container, debugDescription:
3333 " Expected date to be in ISO8601 date format, but ` \( dateString) ` is not in the correct format " )
3434 }
3535 self . wrappedValue = date
3636 }
3737
38- private static func decodeDate( from string: String ) -> Date ? {
39- #if os(Linux)
40- return Self . dateFormatter. date ( from: string)
41- #elseif os(macOS)
42- if #available( macOS 10 . 12 , * ) {
43- return Self . dateFormatter. date ( from: string)
44- } else {
45- // unlikely *debugging* use case of swift 5.2+ on older macOS
46- preconditionFailure ( " Unsporrted macOS version " )
47- }
48- #endif
49- }
38+ private static let dateFormatter : DateFormatter = Self . createDateFormatter ( )
5039
51- @available ( macOS 10 . 12 , * )
52- private static let dateFormatter = ISO8601DateFormatter ( )
40+ private static func createDateFormatter( ) -> DateFormatter {
41+ let formatter = DateFormatter ( )
42+ formatter. locale = Locale ( identifier: " en_US_POSIX " )
43+ formatter. timeZone = TimeZone ( secondsFromGMT: 0 )
44+ formatter. dateFormat = " yyyy-MM-dd'T'HH:mm:ssZZZZZ "
45+ return formatter
46+ }
5347}
5448
5549@propertyWrapper
@@ -63,39 +57,20 @@ public struct ISO8601WithFractionalSecondsCoding: Decodable {
6357 public init ( from decoder: Decoder ) throws {
6458 let container = try decoder. singleValueContainer ( )
6559 let dateString = try container. decode ( String . self)
66- guard let date = Self . decodeDate ( from: dateString) else {
60+ guard let date = Self . dateFormatter . date ( from: dateString) else {
6761 throw DecodingError . dataCorruptedError ( in: container, debugDescription:
6862 " Expected date to be in ISO8601 date format with fractional seconds, but ` \( dateString) ` is not in the correct format " )
6963 }
7064 self . wrappedValue = date
7165 }
7266
73- private static func decodeDate( from string: String ) -> Date ? {
74- #if os(Linux)
75- return Self . dateFormatter. date ( from: string)
76- #elseif os(macOS)
77- if #available( macOS 10 . 13 , * ) {
78- return self . dateFormatter. date ( from: string)
79- } else {
80- // unlikely *debugging* use case of swift 5.2+ on older macOS
81- preconditionFailure ( " Unsporrted macOS version " )
82- }
83- #endif
84- }
85-
86- @available ( macOS 10 . 13 , * )
87- private static let dateFormatter : ISO8601DateFormatter = Self . createDateFormatter ( )
67+ private static let dateFormatter : DateFormatter = Self . createDateFormatter ( )
8868
89- @available ( macOS 10 . 13 , * )
90- private static func createDateFormatter( ) -> ISO8601DateFormatter {
91- let formatter = ISO8601DateFormatter ( )
92- formatter. formatOptions = [
93- . withInternetDateTime,
94- . withDashSeparatorInDate,
95- . withColonSeparatorInTime,
96- . withColonSeparatorInTimeZone,
97- . withFractionalSeconds,
98- ]
69+ private static func createDateFormatter( ) -> DateFormatter {
70+ let formatter = DateFormatter ( )
71+ formatter. locale = Locale ( identifier: " en_US_POSIX " )
72+ formatter. timeZone = TimeZone ( secondsFromGMT: 0 )
73+ formatter. dateFormat = " yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ "
9974 return formatter
10075 }
10176}
0 commit comments