-
Notifications
You must be signed in to change notification settings - Fork 542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
%Z can't print the timezone abbreviation #960
Comments
This issue doesn't come with a reproducible example, nor does it show the actual output. Please extend your bug report. |
@djc An example is like following use chrono::{format::StrftimeItems, Local};
fn main() {
let now = Local::now();
now.with_timezone(now.offset());
let formatted = now
.format_with_items(StrftimeItems::new("%a %b %d %I:%M:%S '%Z' %Y"))
.to_string();
println!("{formatted}");
} expected result
actual result
The expected abbreviation |
chrono itself doesn't know timezones beyond their offsets, so I don't think it can do what you want. Maybe look at the chrono-tz crate and see if it can help with this? |
@howjmay - please see also this issue: #749.
let tz_str = iana_time_zone::get_timezone()?;
let tz: chrono_tz::Tz = tz_str.parse()?; There is also a partially complete PR to solve this #750 and we would appreciate any thoughts or comments you have on it. |
Hi @esheppa the method you provided is good. |
chrono-tz includes data from the IANA database. It's definitely possible that this doesn't encompass all abbreviations listed on Wikipedia. I think it is reasonable to consider the IANA database authoritative in this regard. |
thanks! |
While this being an issue is understandable, the documentation makes it seem like https://docs.rs/chrono/latest/chrono/format/strftime/index.html There are even footnotes for this specifier but none that say it isn't supported for formatting. If possible, I would love the documentation to reflect this. Thanks for your time. |
@campbellcole Would you be willing to make a PR? |
Sure! Will do that tomorrow evening if that’s alright with you.
…On Fri, May 5, 2023 at 9:44 AM Paul Dicker ***@***.***> wrote:
@campbellcole <https://github.com/campbellcole> Would you be willing to
make a PR?
—
Reply to this email directly, view it on GitHub
<#960 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACPSNYR7LDDQEM44VGO6TTTXEUU6HANCNFSM6AAAAAAU2A5ZPA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
For anyone finding this issue from the documentation, you can accomplish this using two additional crates ( use chrono::{TimeZone, Utc};
use chrono_tz::{OffsetName, Tz};
use iana_time_zone::get_timezone;
let tz_str = get_timezone().unwrap();
let tz: Tz = tz_str.parse().unwrap();
let offset = tz.offset_from_utc_date(&Utc::now().date_naive());
let abbreviation = offset.abbreviation();
println!("{abbreviation}"); |
Better documented in #1051. |
Hi I am having a use case that I need to generate formatted time, and the text should contain the abbreviation of the timezone.
I am using
format_with_items()
, and a format string"%a %b %d %I:%M:%S '%Z' %p %Y"
. However,%Z
doesn't return the abbreviation. What should I do?The text was updated successfully, but these errors were encountered: