Skip to content

Commit

Permalink
Update date formatter to use fractional seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrankriyam committed Nov 13, 2024
1 parent d324b8f commit 17760fe
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions Sources/SakuraKit/Play/PlayAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,21 @@ private extension Data {
private extension JSONDecoder {
static var playDateDecoder: JSONDecoder {
let decoder = JSONDecoder()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
formatter.timeZone = TimeZone(abbreviation: "UTC")
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]

decoder.dateDecodingStrategy = .formatted(formatter)
decoder.dateDecodingStrategy = .custom { decoder in
let container = try decoder.singleValueContainer()
let dateStr = try container.decode(String.self)

guard let date = formatter.date(from: dateStr) else {
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "Invalid date format: \(dateStr)"
)
}
return date
}
return decoder
}
}

0 comments on commit 17760fe

Please sign in to comment.