vincentneo / CoreGPX

A library for parsing and creation of GPX location files. Purely Swift.
https://www.vincent-neo.com
MIT License
261 stars 57 forks source link

Dates with fractional seconds cannot be parsed on Linux #100

Open georgbachmann opened 1 year ago

georgbachmann commented 1 year ago

I have a service where I would like to use CoreGPX and noticed, that there is a difference in date-parsing between local development on my Mac and running it on linux. I have a GPX file with a date like so: <time>2023-04-25T07:58:06.001Z</time> (seems like Polar does that) and those cannot be parsed. When I remove the fractional seconds from that date string (like so: <time>2023-04-25T07:58:06Z</time> everything works fine.

So I did some digging and found that CoreGPX is using

        #if os(Linux)
        return ISO8601DateFormatter().date(from: NonNilString)
        #else

That ISO8601DateFormatter wold have the option to dateFormatter.formatOptions.insert(.withFractionalSeconds) and then it would parse the date.... BUT :) it will not parse dates any more, that don't have fractional seconds.

I don't know that much about dateformatters and if there is an option to tell it to try both... but maybe we could have two formatters? The one without and then fallback to the one with fractional seconds? I wasn't too sure which way to go, so I thought raising an issue before doing a PR to discuss it.

vincentneo commented 1 year ago

Apologies @georgbachmann, I didn't see this until today.

The thing with DateFormatters in my opinion are that they are pretty slow, at least in the context where you need to parse like hundreds or thousands of dates, so I'm not sure if a fallback would be a good idea.

I think having an option would be better because of that.