Skip to content

Add support for "date" and "date-time" with fractional seconds decoding in accordance with RFC 3339 #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Sources/OpenAPIRuntime/Conversion/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ public struct ISO8601DateTranscoder: DateTranscoder {

/// Creates and returns a date object from the specified ISO 8601 formatted string representation.
public func decode(_ dateString: String) throws -> Date {
guard let date = ISO8601DateFormatter().date(from: dateString) else {
let iso8601DateFormatter = ISO8601DateFormatter()

if dateString.contains(".") {
iso8601DateFormatter.formatOptions = [.withFractionalSeconds, .withInternetDateTime]
} else if dateString.range(of: #"^\d{4}-\d{2}-\d{2}$"#, options: .regularExpression, range: nil, locale: nil) != nil {
iso8601DateFormatter.formatOptions = [.withFullDate]
}

guard let date = iso8601DateFormatter.date(from: dateString) else {
throw DecodingError.dataCorrupted(
.init(
codingPath: [],
Expand Down