14
14
15
15
import struct Foundation. Date
16
16
import class Foundation. DateFormatter
17
- import class Foundation. ISO8601DateFormatter
18
17
import struct Foundation. Locale
18
+ import struct Foundation. TimeZone
19
19
20
20
@propertyWrapper
21
21
public struct ISO8601Coding : Decodable {
@@ -28,28 +28,22 @@ public struct ISO8601Coding: Decodable {
28
28
public init ( from decoder: Decoder ) throws {
29
29
let container = try decoder. singleValueContainer ( )
30
30
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 {
32
32
throw DecodingError . dataCorruptedError ( in: container, debugDescription:
33
33
" Expected date to be in ISO8601 date format, but ` \( dateString) ` is not in the correct format " )
34
34
}
35
35
self . wrappedValue = date
36
36
}
37
37
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 ( )
50
39
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
+ }
53
47
}
54
48
55
49
@propertyWrapper
@@ -63,39 +57,20 @@ public struct ISO8601WithFractionalSecondsCoding: Decodable {
63
57
public init ( from decoder: Decoder ) throws {
64
58
let container = try decoder. singleValueContainer ( )
65
59
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 {
67
61
throw DecodingError . dataCorruptedError ( in: container, debugDescription:
68
62
" Expected date to be in ISO8601 date format with fractional seconds, but ` \( dateString) ` is not in the correct format " )
69
63
}
70
64
self . wrappedValue = date
71
65
}
72
66
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 ( )
88
68
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 "
99
74
return formatter
100
75
}
101
76
}
0 commit comments