File tree Expand file tree Collapse file tree 3 files changed +7
-18
lines changed Expand file tree Collapse file tree 3 files changed +7
-18
lines changed Original file line number Diff line number Diff line change @@ -11,30 +11,17 @@ import Foundation
11
11
// encapsluates the metar string date in order to faciliate the JSON parsing
12
12
struct MetarDate : Decodable {
13
13
let date : Date
14
-
15
14
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 "
20
19
}
21
20
init ( from decoder: Decoder ) throws {
22
21
// The metar time has a format 'ddHHmmZ' (e.g 130756Z)
23
22
// So we need to append the current year and month (yyyy-MM) in order to parse the entire date back
24
23
25
24
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)
39
26
}
40
27
}
Original file line number Diff line number Diff line change @@ -59,6 +59,7 @@ public struct AVWXClient {
59
59
return
60
60
}
61
61
let decoder = JSONDecoder ( )
62
+ decoder. dateDecodingStrategy = . iso8601
62
63
do {
63
64
let result = try decoder. decode ( T . self, from: data)
64
65
completion ( Result . success ( result) )
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ import XCTest
15
15
class DeserialisationSpecs : QuickSpec {
16
16
override func spec( ) {
17
17
let decoder = JSONDecoder ( )
18
+ decoder. dateDecodingStrategy = . iso8601
18
19
19
20
describe ( " metar parsing " ) {
20
21
context ( " given a valid metar " ) {
You can’t perform that action at this time.
0 commit comments