Skip to content

Commit ef5017d

Browse files
committed
use iso date for decoding
1 parent bcefae8 commit ef5017d

File tree

3 files changed

+7
-18
lines changed

3 files changed

+7
-18
lines changed

Sources/AVWXKit/Models/MetarDate.swift

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,17 @@ import Foundation
1111
// encapsluates the metar string date in order to faciliate the JSON parsing
1212
struct MetarDate: Decodable {
1313
let date: Date
14-
1514
enum CodingKeys: String, CodingKey {
16-
// todo
17-
// "repr": "121756Z",
18-
// "dt": "2019-04-12T17:56:00Z"
19-
case time = "repr"
15+
// "121756Z",
16+
case repr = "repr"
17+
// "2019-04-12T17:56:00Z"
18+
case dt = "dt"
2019
}
2120
init(from decoder: Decoder) throws {
2221
// The metar time has a format 'ddHHmmZ' (e.g 130756Z)
2322
// So we need to append the current year and month (yyyy-MM) in order to parse the entire date back
2423

2524
let container = try decoder.container(keyedBy: CodingKeys.self)
26-
let metarString = try container.decode(String.self, forKey: .time)
27-
28-
let now = Date()
29-
let formatter = DateFormatter()
30-
31-
formatter.dateFormat = "yyyy-MM"
32-
33-
let thisMonth = formatter.string(from: now)
34-
// yy-MM-ddHHmmZ
35-
let metarFullDateString = thisMonth + "-" + metarString
36-
37-
formatter.dateFormat = "yy-MM-ddHHmmZ"
38-
date = formatter.date(from: metarFullDateString)!
25+
date = try container.decode(Date.self, forKey: .dt)
3926
}
4027
}

Sources/AVWXKit/Network/AVWXClient.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public struct AVWXClient {
5959
return
6060
}
6161
let decoder = JSONDecoder()
62+
decoder.dateDecodingStrategy = .iso8601
6263
do {
6364
let result = try decoder.decode(T.self, from: data)
6465
completion(Result.success(result))

Tests/AVWXKitTests/DeserialisationSpecs.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import XCTest
1515
class DeserialisationSpecs: QuickSpec {
1616
override func spec() {
1717
let decoder = JSONDecoder()
18+
decoder.dateDecodingStrategy = .iso8601
1819

1920
describe("metar parsing") {
2021
context("given a valid metar") {

0 commit comments

Comments
 (0)