google/cctz

Week parsing (%W, %w) does not work

tudor opened this issue · 5 comments

tudor commented

Parsing 2020-05-1 using the %Y-%W-%w (year, week number, day in week) doesn't work; it returns the date 2020/01/01 instead of 2020/02/03` (Y/M/D).

This is because cctz doesn't handle %W and %w internally, but passes them to strptime one at a time, however strptime does nothing if it only sees %W, it needs both %W and %w in order to return something. (And presumably %Y as well in order to know whether the current year is a leap year or not.)

Acknowledged. This is referred to in the parse() documentation ...

parse() only heeds the fields year, month, day, hour, minute, (fractional) second, and UTC offset. Other fields ... are ignored in the conversion.

but I agree that the limitation could be made clearer.

Aside: There is also a reference to the issue in src/time_zone_format_test.cc

  // Because we handle each (non-internal) specifier in a separate call
  // to strptime(), there is no way to group %C and %y together.

As for a "fix", I'm not sure what to suggest beyond trying to use strptime() directly (despite its shortcomings). The std::tm result can be converted into a cctz::civil_second, which can then be passed to cctz::time_zone::lookup() to get a time_point.

If there are different/better ideas, I'm all ears.

Thanks.

If there are different/better ideas, ...

I guess we could try to handle %W/%w internally too.

tudor commented

I would probably move away from strptime, but gah that code is ugly to write.

tudor commented

It would address #86 too, two birds with one stone? :)

Sorry for the delay @tudor.

Please confirm that this behaves as you expect, and reopen if not.

Thanks.