Skip to content

Commit 80fa3c7

Browse files
committed
Use ISO8601FormatStyle on Linux
1 parent e815a09 commit 80fa3c7

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

Sources/ReerJSON/JSONDecoderImpl.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,21 @@ final class JSONDecoderImpl: Decoder {
260260
return Date(timeIntervalSince1970: double / 1000.0)
261261
case .iso8601:
262262
let string = try unboxString(from: value, for: codingPathNode, additionalKey)
263+
#if os(Linux)
264+
guard let date = try? Date.ISO8601FormatStyle().parse(string) else {
265+
throw DecodingError.dataCorrupted(.init(
266+
codingPath: codingPathNode.path(byAppending: additionalKey),
267+
debugDescription: "Expected date string to be ISO8601-formatted."
268+
))
269+
}
270+
#else
263271
guard let date = _iso8601Formatter.date(from: string) else {
264272
throw DecodingError.dataCorrupted(.init(
265273
codingPath: codingPathNode.path(byAppending: additionalKey),
266274
debugDescription: "Expected date string to be ISO8601-formatted."
267275
))
268276
}
277+
#endif
269278
return date
270279
case .formatted(let formatter):
271280
let string = try unboxString(from: value, for: codingPathNode, additionalKey)

Sources/ReerJSON/Utilities.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,7 @@ extension Optional {
146146
}
147147
}
148148

149-
#if os(Linux)
150-
nonisolated(unsafe) let _iso8601Formatter: ISO8601DateFormatter = {
151-
let formatter = ISO8601DateFormatter()
152-
formatter.formatOptions = .withInternetDateTime
153-
return formatter
154-
}()
155-
#else
149+
#if !os(Linux)
156150
nonisolated(unsafe) let _iso8601Formatter: JJLISO8601DateFormatter = {
157151
let formatter = JJLISO8601DateFormatter()
158152
formatter.formatOptions = .withInternetDateTime

0 commit comments

Comments
 (0)