ISO8601DateFormatter fails to parse a valid ISO-8601 date

I solved this problem by writing my own date formatted. To answer your questions:

Let's first look at the implementation of DateEncodingStrategy:

/// Encode the `Date` as an ISO-8601-formatted string (in RFC 3339 format).
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
case iso8601

You need these 3 pieces of information to understand what it means:

  1. What is the difference between ISO 8601 and RFC 3339?

RFC 3339 is listed as a profile of ISO 8601 link

  1. What does profile mean?

A Profile of ISO 8601 is a specification developed by a particular community which explains how ISO 8601 is to be used, to carry out a particular function or group of functions relevant to that community. link

So in Swift we are using an implementation of RFC3339.

  1. OK, but why doesn't JSONDecoder support fractions of a second?
    In section 5.6 of RFC3339, you can find the exact specification of a valid Internet Date/Time format and in this specification time-secfrac is optional:
partial-time    = time-hour ":" time-minute ":" time-second[time-secfrac]

This part of RFC may justify why they decided not to support this optional part:

5.3. Rarely Used Options
...
The format defined below includes only one rarely used option:
fractions of a second. It is expected that this will be used only by
applications which require strict ordering of date/time stamps or
which have an unusual precision requirement.

Though, based on my day-to-day experience fractions of a second is not rare at all.

1 Like