-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds ISO8601WithFractionalSecondsStrategy
- Loading branch information
Showing
2 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
Sources/BetterCodable/ISO8601WithFractionalSecondsStrategy.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Foundation | ||
|
||
/// Decodes `String` values as an ISO8601 `Date`. | ||
/// | ||
/// `@ISO8601Date` relies on an `ISO8601DateFormatter` in order to decode `String` values into `Date`s. Encoding the `Date` | ||
/// will encode the value into the original string value. | ||
/// | ||
/// For example, decoding json data with a `String` representation of `"1996-12-19T16:39:57-08:00"` produces a valid `Date` | ||
/// representing 39 minutes and 57 seconds after the 16th hour of December 19th, 1996 with an offset of -08:00 from UTC | ||
/// (Pacific Standard Time). | ||
@available(iOS 11.0, tvOS 11.0, watchOS 4.0, macOS 10.13, *) | ||
public struct ISO8601WithFractionalSecondsStrategy: DateValueCodableStrategy { | ||
private static let formatter: ISO8601DateFormatter = { | ||
let formatter = ISO8601DateFormatter() | ||
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds] | ||
return formatter | ||
}() | ||
|
||
public static func decode(_ value: String) throws -> Date { | ||
guard let date = Self.formatter.date(from: value) else { | ||
throw DecodingError.dataCorrupted(.init(codingPath: [], debugDescription: "Invalid Date Format!")) | ||
} | ||
return date | ||
} | ||
|
||
public static func encode(_ date: Date) -> String { | ||
return Self.formatter.string(from: date) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters