Skip to content
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

Fix date decoding error #140

Merged
merged 4 commits into from
May 5, 2021
Merged
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
5 changes: 5 additions & 0 deletions Sources/APIProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ public final class APIProvider {
/// Contains a JSON Decoder which can be reused.
static let jsonDecoder: JSONDecoder = {
let decoder = JSONDecoder()

let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.calendar = Calendar(identifier: .iso8601)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be enough to set this calendar? Do we really need the timezone and locale setting?

Copy link
Author

@avsadyryn avsadyryn May 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we set only the calendar parameter, then locale and timeZone will stay by default (according to the user iOS Settings). And for ISO8601-like DateFormatter it's expected en_US_POSIX locale and GMT timeZone. I think it would be more stable that way.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, makes sense! Thanks for the detailed explanation 🙂


decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in
let container = try decoder.singleValueContainer()
let dateStr = try container.decode(String.self)
Expand Down