Possible bug in Swift Decoder for Date type values

Hi! I've just witnessed a possible bug in decoding JSON from the server.

public struct MyNotification: Equatable, Decodable {

var notificationId: String
var title: String
var content: String
var date: Date
var status: MyNotificationStatus

private enum CodingKeys: String, CodingKey {
    case notificationId = "id"
    case title = "titulo"
    case content = "conteudo"
    case status = "status"
    case date = "dataCriacao"
}

public init(from decoder: Decoder) throws {

    let container = try decoder.container(keyedBy: CodingKeys.self)
    notificationId = try container.decode(String.self, forKey: .notificationId)
    title = try container.decode(String.self, forKey: .title)
    content = try container.decode(String.self, forKey: .content)

Does NOT work properly:
date = try container.decode(Date.self, forKey: .date)

Does parse the correct date:
let dateInt = try container.decode(Int.self, forKey: .date)
date = Date(timeIntervalSince1970: TimeInterval(dateInt))

If I try to parse directly to a Date object a normal epoch timestamp eg.: 1606314559 it parses as 2051-11-26 and using the Int approach it parses correctly ( 2020-11-25 14:29:19 +0000 )

Am I missing something here? Shouldn't date = try container.decode(Date.self, forKey: .date) already detect the correct date format and parse accordingly?

Can you show us a sample of the JSON it is parsing?

It's not a bug.
The default behavior of Decoder is to use timeIntervalSinceReferenceDate not timeIntervalSince1970

try

let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970
3 Likes

sure. it was:

{"notificacoes":[{"id":"e14c5bde-e303-4943-b054-d03cfb556690","titulo":"Título 2","conteudo":"Conteúdo 2","status":"NAO_LIDA","dataCriacao":1606314559},{"id":"1802ab3b-9864-43f1-b1fa-e7bb23dece87","titulo":"Título 1","conteudo":"Conteúdo 1","status":"NAO_LIDA","dataCriacao":1606314530}]}

Thank you! I was wondering which was the default