chrono
chrono copied to clipboard
Date formatting %Z doesn't print name of local time zone
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
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
I ran into this as well. I'll probably take a stab at submitting the pull request for this.
that would be very welcome!
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
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?
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
Does any one know any workarounds?
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.
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.
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"