Hello,
My code calls a macOS system library which returns Foundation Date
properties. I have a program that will run every night, and output the data via the Swift JSONEncoder
and uses DateEncodingStrategy.iso8601
. As you likely know, a DST shift happened over the weekend here in the US. In my output, every single Date
changed by 1 hour, despite the fact that nothing in the underlying data changed overnight. Here is an example diff in the output. I see the "Z", which I think should not be affected by DST changes.
- "dateAdded" : "2003-12-15T17:02:56Z",
- "dateModified" : "2007-03-07T04:31:16Z",
+ "dateAdded" : "2003-12-15T18:02:56Z",
+ "dateModified" : "2007-03-07T05:31:16Z",
Here is a sample of the data:
public struct Track: Codable, Hashable {
var dateAdded: Date?
var dateModified: Date?
}
And the encoding is here:
extension Array where Element == Track {
public func jsonData() throws -> Data {
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
encoder.dateEncodingStrategy = .iso8601
return try encoder.encode(self)
}
}
Pretty basic stuff overall. So my questions are:
- Am I correct in my assumption that
.iso8601
is UTC, and that UTC is daylight savings shift agnostic? - Is this the right way to ensure the my JSON is encoded in UTC?
- If the library I am calling is building its
Date
incorrectly, how may I work around the problem?
I'm not reporting the library name right now, in order to ensure that my code is doing the right thing without assumptions.
Thanks for any tips!