-
Notifications
You must be signed in to change notification settings - Fork 15
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
Setting the time zone via the TZ variable has no effect #118
Comments
What platform are you on? How do you test this? On my PC:
|
I'm on Debian/Linux, mostly Bookworm (i.e. current stable):
|
This crate developed organically and had no unifying concept behind handling the According to the POSIX specification, there are two options for the It seems glibc, musl, Android, and macOS (at least) all follow a similar convention in which the local IANA time zone can be specified in the
In current behavior of this crate is as follows. On some platforms (e.g. macOS) we use a function call to a system library to get the time zone name for us. In the case of macOS, this will handle the TZ environment variable for us (although see below). In the case of linux, we examine the Should we handle the Other notes:In testing, I found that in macOS 13.5.1, the function Apparently C++20 has added I have not yet attempted to figure out what Windows does with the TZ environment variable. I did find this https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/tzset?view=msvc-170. |
I concur with your analysis. Personally, I think this crate should strive for consistency with the platform, so that applications built on top of it behave like "native" applications, i.e. avoid surprising the user. |
Hey folks, can y'all help me understand how making this behavior change would not be the same as reintroducing https://rustsec.org/advisories/RUSTSEC-2020-0159.html to the ecosystem? |
@lopopolo I think if we use rust's std to get the environment variable, we should be OK (see time-rs/time#293 (comment)). That said, I'm not an expert in this. |
I have the same problem, my work environment is Debian:bookworm (both docker image and wsl2), I found set TZ variable has no effect on my code, my code and my test step as follows: The 'date' command can return the right timezone. ls -l /etc/localtime; # /etc/localtime -> /usr/share/zoneinfo/Etc/UTC
echo "TZ='Asia/Shanghai';export TZ;" >> ~/.profile;
source ~/.profile;
echo $TZ; # Asia/Shanghai
date; # Sun Nov 24 10:31:17 PM CST 2024 My rust code could not. use chrono::{Local, TimeZone};
use chrono_tz::Tz;
fn main() {
let tz = iana_time_zone::get_timezone().unwrap();
println!("🚀 {:?}", tz); // 🚀 "Etc/UTC"
let tz = tz.parse::<Tz>().unwrap();
// let tz = Tz::Asia__Shanghai; // my real TZ
let dt = tz.from_local_datetime(&Local::now().naive_local()).unwrap();
println!("{}", dt.format("%a %b %e %r %Z %Y").to_string()); // Sun Nov 24 10:27:33 PM UTC 2024
} I will subscribe this issue and have a good day. |
…one() can't get the right Tz. See [here](strawlab/iana-time-zone#118) for more details.
…ne() can't get the right Tz. See [here](strawlab/iana-time-zone#118) for more details.
On POSIX systems, the timezone can be overridden on a per-process basis by setting the
TZ
environment variable:Unfortunately, this has no effect on this crate.
The text was updated successfully, but these errors were encountered: