chrono icon indicating copy to clipboard operation
chrono copied to clipboard

Date formatting %Z doesn't print name of local time zone

Open silverbucket opened this issue 6 years ago • 10 comments

In the documentation for format::strftime %Z is supposed to print the name of the local time zone (e.g. CET), but instead it prints +1:00, which should be displayed with %:z.

example:

...
extern crate chrono;
use chrono::prelude::*;
...
let dt = Local::now();
println!("{}", dt.format("%a %e %b %T %Z %Y").to_string());

prints: Thu 8 Nov 01:06:00 +01:00 2018

silverbucket avatar Nov 08 '18 00:11 silverbucket

Parsing also fails

DateTime::parse_from_str("2018-08-08 10:10:10 GMT", "%Y-%m-%d %H:%M:%S %Z")
// bad or unsupported format string

aswinkarthik avatar Nov 10 '18 18:11 aswinkarthik

I ran into this as well. I'll probably take a stab at submitting the pull request for this.

jmqd avatar Mar 11 '19 13:03 jmqd

that would be very welcome!

quodlibetor avatar Mar 11 '19 15:03 quodlibetor

I also just ran into this (on Arch Linux if that's relevant)

Note that the documentation makes it sound like it will be the name

spease avatar Aug 26 '19 00:08 spease

Is this a bug in chrono or somewhere else. I tried to find the bug, but i don't know where to start looking for it. Can the maintainer at least point to the file where to start?

kegesch avatar Oct 07 '19 16:10 kegesch

I looked into this once and it looks like the fmt::Display trait for FixedOffset does not return the time zone but just the Debug-Format: https://github.com/chronotope/chrono/blob/9e50bfe03460036dbd752440851d924be3730b08/src/offset/fixed.rs#L136

This is not easy so solve correctly. The problem with translating the offset to a time zone is that its ambiguous. (See: https://en.wikipedia.org/wiki/List_of_time_zone_abbreviations) I even looked up, how the glibc implementation tries to solve it and it was a mess. My suggestion is, to remove the %Z option. Funny thing is, that its (failing) test is already commented out: https://github.com/chronotope/chrono/blob/9e50bfe03460036dbd752440851d924be3730b08/src/format/strftime.rs#L468

picsel2 avatar Oct 08 '19 07:10 picsel2

Does any one know any workarounds?

danobi avatar Sep 09 '20 21:09 danobi

Does any one know any workarounds?

The "most correct" answer -- and the reason that I believe that there isn't constant outrage that this isn't implemented in chrono or any other dt library that I know of -- is to always store and serialize times in utc, and consider timezone an entirely separate concept, and serialize them separately. This means, for example, something like "event created at

We could, potentially, create some sort of chrono-specific extension that allows formatting a DateTime with an extended tz ID, but I'd be hesitant to be the first datetime library (or language spec) to implement that, so pointers to where other folks have felt like it was the right decision would be appreciated.

quodlibetor avatar Sep 09 '20 23:09 quodlibetor

Worth pointing out related to this, though, is that %Z (as of chrono 0.4.13) follows glibc and accepts %Z as an ignore flag.

quodlibetor avatar Sep 10 '20 02:09 quodlibetor

is to always store and serialize times in utc, and consider timezone an entirely separate concept, and serialize them separately.

Agreed. Our application stores everything as unix time. We only need the time zone stuff during display time in the UI.

Worth pointing out related to this, though, is that %Z (as of chrono 0.4.13) follows glibc and accepts %Z as an ignore flag.

Can you clarify what an ignore flag is? Didn't find anything doing a quick google search.

%Z seems to work ok on my system (using example program at bottom of man strftime):

$ ./a.out "%Z"
Result string is "PDT"

danobi avatar Sep 10 '20 17:09 danobi