Reputation: 28094
It's trivial to find libraries which say they can parse a time or date and time presented in ISO8601. What they all seem to mean is that they can be configured to parse one format of ISO8601, for instance by hand-rolling a suitable string format code.
But in fact ISO8601 defines a wide variety of possible formats for representing date and time. For instance, to give a very short list of variations:
You could almost say, ISO8601 defines an entire DSL for representing dates and times. If you only know a string is an ISO8601-formatted time, you don't know much.
So what I'm wondering is this: Can you read a string, knowing only that it is encoded in some ISO8601 format, and reliably infer which format it is using? Or are the ISO8601 formats unfortunately defined in such a way that a parser needs to know ahead of time which format is being used?
Upvotes: 2
Views: 419
Reputation: 241535
It's an interesting question, but the answer is invariably no. To parse a string is not only to evaluate its characters, but to evaluate them with specific intent.
In other words, what would you parse it into? If one has a string that might contain a date, and might contain a time, and might contain an offset from UTC, or might contain a week-of-the-year based value, or might contain a period of elapsed time, and might contain a range of such periods, possibly with a fixed point at one end... Such a resulting object that could parse all of them would not be very useful. One would have to test a lot of optional properties of that object in order to know how to use it. Perhaps one might use such an object for validation of the string being in one of a handful of valid formats, but regular expressions are much better suited for that task.
It's worth mentioning that languages that have tried to make one-date-time-type-to-rule-them-all usually have ended up regretting it. Consider java.util.Date
which fell into this trap. Java developers should know by now that they should be using java.time
or Joda-Time instead. Also consider schema languages like XSD, which have a wide variety of date and time types defined, many of which are ISO8601 compatible. If there was only one type, it would devalue the purpose of defining a schema.
Upvotes: 3