Skip to content

Commit

Permalink
Fixes date parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mmllr committed Jan 25, 2022
1 parent 48ac2aa commit 0e13713
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 21 deletions.
9 changes: 2 additions & 7 deletions Sources/GPXKit/GPXExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import FoundationXML

/// A class for exporting a `GPXTrack` to an xml string.
public final class GPXExporter {
private lazy var iso8601Formatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = .withInternetDateTime
return formatter
}()
private let track: GPXTrack
private let exportDate: Bool

Expand Down Expand Up @@ -45,7 +40,7 @@ public final class GPXExporter {

private var metaDataTime: String {
guard exportDate, let date = track.date else { return "" }
return GPXTags.time.embed(iso8601Formatter.string(from: date))
return GPXTags.time.embed(ISO8601DateFormatter.gpxKit.string(from: date))
}

private var trackXML: String {
Expand All @@ -58,7 +53,7 @@ public final class GPXExporter {
].joined(separator: " ")
let childs = [GPXTags.elevation.embed(String(format:"%.2f", point.coordinate.elevation)),
exportDate ? point.date.flatMap {
GPXTags.time.embed(iso8601Formatter.string(from: $0))
GPXTags.time.embed(ISO8601DateFormatter.gpxKit.string(from: $0))
} : nil
].compactMap { $0 }.joined(separator: "\n")
return GPXTags.trackPoint.embed(
Expand Down
8 changes: 1 addition & 7 deletions Sources/GPXKit/GPXFileParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,6 @@ internal extension TrackPoint {
}

internal extension XMLNode {
static var iso8601Formatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = .withInternetDateTime
return formatter
}()

var latitude: Double? {
Double(attributes[GPXAttributes.latitude.rawValue] ?? "")
}
Expand All @@ -184,7 +178,7 @@ internal extension XMLNode {
Double(content)
}
var date: Date? {
XMLNode.iso8601Formatter.date(from: content)
ISO8601DateFormatter.gpxKit.date(from: content)
}
var power: Measurement<UnitPower>? {
Double(content).flatMap {
Expand Down
13 changes: 13 additions & 0 deletions Sources/GPXKit/ISO8601DateFormatter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation

extension ISO8601DateFormatter {
static var gpxKit: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
if #available(macOS 10.13, *) {
formatter.formatOptions = .withFractionalSeconds
} else {
formatter.formatOptions = .withInternetDateTime
}
return formatter
}()
}
2 changes: 1 addition & 1 deletion Tests/GPXKitTests/GPXExporterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class GPXExporterTests: XCTestCase {
private var parseError: GPXParserError?
private var iso8601Formatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = .withInternetDateTime
formatter.formatOptions = .withFractionalSeconds
return formatter
}()
private var result: GPXKit.XMLNode!
Expand Down
4 changes: 2 additions & 2 deletions Tests/GPXKitTests/GPXParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ class GPXParserTests: XCTestCase {

let expected = [
TrackPoint(coordinate: Coordinate(latitude: 51.2760600, longitude: 12.3769500, elevation: 114.2),
date: expectedDate(for: "2020-03-18T12:39:47Z")),
date: expectedDate(for: "2020-07-03T13:20:50.000Z")),
TrackPoint(coordinate: Coordinate(latitude: 51.2760420, longitude: 12.3769760, elevation: 114.0),
date: expectedDate(for: "2020-03-18T12:39:48Z"))
date: expectedDate(for: "2020-03-18T12:45:48Z"))
]

assertTracksAreEqual(GPXTrack(date: expectedDate(for: "2020-03-18T12:39:47Z"),
Expand Down
6 changes: 3 additions & 3 deletions Tests/GPXKitTests/TestFixtures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ let testXMLWithoutExtensions = """
<trkseg>
<trkpt lat="51.2760600" lon="12.3769500">
<ele>114.2</ele>
<time>2020-03-18T12:39:47Z</time>
<time>2020-07-03T13:20:50.000Z</time>
</trkpt>
<trkpt lat="51.2760420" lon="12.3769760">
<ele>114.0</ele>
<time>2020-03-18T12:39:48Z</time>
<time>2020-03-18T12:45:48Z</time>
</trkpt>
</trkseg>
</trk>
Expand Down Expand Up @@ -107,7 +107,7 @@ let testTrack = GPXTrack(date: expectedDate(for: "2020-03-18T12:39:47Z"),
TrackPoint(coordinate: Coordinate(latitude: 51.2760600, longitude: 12.3769500, elevation: 114.2),
date: expectedDate(for: "2020-03-18T12:39:47Z")),
TrackPoint(coordinate: Coordinate(latitude: 51.2760420, longitude: 12.3769760, elevation: 114.0),
date: expectedDate(for: "2020-03-18T12:39:48Z"))
date: expectedDate(for: "2020-03-18T12:45:48Z"))
])

let testTrackWithoutTime = GPXTrack(date: nil,
Expand Down
2 changes: 1 addition & 1 deletion Tests/GPXKitTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public func XCTAssertEqual<T: Equatable>(_ expected: @autoclosure () throws -> T

fileprivate var iso8601Formatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = .withInternetDateTime
formatter.formatOptions = .withFractionalSeconds
return formatter
}()

Expand Down

0 comments on commit 0e13713

Please sign in to comment.