chrono icon indicating copy to clipboard operation
chrono copied to clipboard

%Z is not supported in parsing but that's unclear in the docs

Open timbertson opened this issue 10 years ago • 4 comments

This works:

DateTime::parse_from_str("2014-09-17 00:00:00 +10:00", "%F %T %z")

But this gives a ParseError(BadFormat):

DateTime::parse_from_str("2014-09-17 00:00:00 AEDT", "%F %T %Z")

timbertson avatar May 24 '15 06:05 timbertson

This is not a bug, timezone name parsing is documented as unsupported. [1] But I guess the strftime docs should have the duplicate information as well. Thank you for feedback.

[1] In general, many timezone abbreviations are ambiguous enough that relying on %Z is useless. AFAIK glibc recognizes %Z but discards its result; if this is desirable, please file a new issue (I was always reluctant about such legacy behavior, but if there are demands, why not).

lifthrasiir avatar May 24 '15 06:05 lifthrasiir

Oh right, that makes sense - I forgot that %Z might be useful for formatting (but not parsing). In that case, yeah - a footnote against the "%Z" in https://lifthrasiir.github.io/rust-chrono/chrono/format/strftime/index.html#specifiers would help.

I'm happy to strip off the timezone myself and then parse it in the Local time. As a sanity check, I then compare Local::now().format("%Z") to the timezone I was given. Unfortunately, "%Z" seems to produce a timezone offset (+10:00) just like "%z" does, rather than a timezone name. I guess it only actually returns a name if you use a named timezone (i.e. UTC) rather than Local.

timbertson avatar May 24 '15 07:05 timbertson

Yes, FixedOffset has a placeholder time zone name. Tzdata support (#23) should give more useful %Z output for Local and others however.

lifthrasiir avatar May 24 '15 07:05 lifthrasiir

I think parsing time zone abbreviations is something we should support at some point. That some are ambiguous does not mean we can't parse any.

Chrono would need a deeper integration with time zones than it currently has (delegating that to chrono_tz or tz_file). So this won't happen anytime soon...

pitdicker avatar Jul 21 '23 10:07 pitdicker