How do I solve the problem of Swift encoding and decoding Date variables with a little loss of precision when using a JSONEncoder/JSONDecoder? After running the Date through with .iso8601 encoding, I find the Date variables are off by under 1 second.
Conversation
Notices
-
Embed this notice
EvilTofu (eviltofu@kopiti.am)'s status on Wednesday, 16-Apr-2025 13:01:53 JST EvilTofu
-
Embed this notice
Dash 🇸🇬 (dashrandom@kopiti.am)'s status on Wednesday, 16-Apr-2025 13:01:53 JST Dash 🇸🇬
@eviltofu short answer, you can't.
Long answer, swift encodes date as a number of seconds. This value is stored as a double. Doubles and floats and anything with decimand places are never 100% accurate.
See: https://stackoverflow.com/questions/4776813/inconsistencies-with-double-data-type-in-c
-
Embed this notice