-
-
Notifications
You must be signed in to change notification settings - Fork 285
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
Provide cached local offset #688
Comments
See #687 (comment). For the same reasoning, I think it is best to not provide this natively. |
I understand. I've published use time_local::OffsetDateTimeExt;
fn main() {
time_local::init();
let date = std::thread::spawn(|| {
// `time::OffsetDateTime::now_local()` will fail because it queries `time::UtcOffset::current_local_time`, instead we can use:
time::OffsetDateTime::now_utc()
.to_local()
.expect("conversion to local offset with cached value should succeed")
})
.join()
.expect("thread should not panic");
println!("{date:?}")
} |
@mickvangelderen Your solution doesn't actually work, because the offset changes over time, so you can't just have 1 offset, you need to look it up for the particular date you want to convert, which brings you back to the env issue. |
Yes, that may be an issue for long running applications. In my case, I have a CLI application that just wants to print a bunch of dates using a reasonable local offset. Using the offset at application startup time is fine. I agree it would be good to document this in the crate. You could argue that the @CryZe what would you do for a short-lived application? |
@CryZe I think that indeed I should remove |
I have rewritten the README and changed the API. The README reads: In order to obtain the local time offset, time calls out to For example, the following single-threaded application has a potential use after free bug: char * value = getenv("KEY"); // obtain pointer
setenv("KEY", "new value"); // potential free
printf("KEY = %s", value); // potential use after free The functions in Rust's Under the assumption that accessing the environment is implemented correctly everywhere for single-threaded programs, there can only be issues in multi-threaded programs. This crate provides a solution for applications that can accept using a cached value of the UTC offset by doing exactly that: caching the UTC offset at the time of invocation. use time_local::{OffsetDateTimeExt, UtcOffsetExt};
fn main() {
time_local::init().expect("initialization should succeed before spawning threads");
let date = std::thread::spawn(|| {
// We can not convert a date time to it's local representation.
assert!(time::OffsetDateTime::now_utc()
.to_local()
.is_err(), "to_local should fail");
// We can use the cached UTC offset computed at application startup. Note that this is computing something
// different entirely, but it may be good enough for your application.
time::OffsetDateTime::now_utc().to_offset(time::UtcOffset::cached_local_offset())
})
.join()
.expect("thread should not panic");
println!("{date:?}")
} Note that a UTC offset depends on both the timezone and a particular date and time. |
First of all, thanks for the
time
library and for teaching me a thing or two (set_env broken, lack of custom type const generics workaround) through its source code.For an application I am working on I want to display
OffsetDateTime
s in the local machine's time zone offset. I found myself writing the following code to make this easy:Caching the value means that it is no longer "current" of course, but it avoids a syscall.
Perhaps having this feature and documenting it would provide a bit more guidance, and and alternative over switching time libraries or enabling unsound calls.
I am wondering if it would make sense to provide this functionality behind a feature flag. Have there been any efforts in this direction already that I missed?
The text was updated successfully, but these errors were encountered: