HowardHinnant / date

A date and time library based on the C++11/14/17 <chrono> header
Other
3.16k stars 685 forks source link

New flag(s) for simple, robust parsing of ISO 8601 date/times (feature request) #687

Open ecorm opened 3 years ago

ecorm commented 3 years ago

As I've reported in #683, parsing ISO 8601 date/time strings via this library (and C++20) is problematic due to the following reasons:

The current set of parsing flags are too rigid and do not allow any valid, arbitrary ISO 8601 timestamp as input. This makes it difficult for a C++ program to interoperate with ISO8601 strings produced by other languages, where the resolution and leap second support may differ.

On the formatting side, the library currently lacks a means of automatically adding the - prefix for dates preceding year 0, as well as the + prefix for for dates after the year 9999 when formatting into an ISO 8601 string. The user is forced to calculate the year and manually add a leading +/- if necessary. The year calculation ends up being computed twice: once by the user, and again by the format function.

ISO 8601 (as well as the closely related RFC3339) is a very common way of exchanging date/times. It should be part of the C++ standard library and be simple to parse/format without having to jump through hoops.

ecorm commented 3 years ago

See https://gist.github.com/ecorm/8878c5075cf8557994983540a13557bc#file-main-cpp as an example of how difficult it currently is to parse an arbitrary ISO 8601 string into an arbitrary time_point.

HowardHinnant commented 3 years ago

The basic design of parse is strptime with just a few extensions which are clear omissions when compared with strftime. For example strftime has %F as an equivalent to %Y-%m-%d, and strptime doesn't, for no apparent reason. So %F is an example of a minor extension supported in parse.

This design is now cemented in C++20. To get something new in parse, it would have to be something I thought had a reasonable chance of going into a future C++ standard. This library serves as a C++ testbed for future standardization possibilities.

ecorm commented 3 years ago

Well, I think a sane way of parsing ISO 8601 should be added to the C++ standard library. Other mainstream languages have an easy way to parse ISO 8601 strings. This library would be the natural place to introduce such a feature. If not parsing flags, then some kind of utility function.

I don't want to pressure you into championing an idea you don't believe in. Is this feature doesn't interest you, I'll understand and won't hold it against you. I suppose I could write up a C++ standard library proposal myself, but I don't like getting involved in politics.

HowardHinnant commented 3 years ago

If you propose something here, I'll certainly consider it.