time-rs / time

The most used Rust library for date and time handling.
https://time-rs.github.io
Apache License 2.0
1.06k stars 261 forks source link

reliable way to get local time #645

Closed kzhui125 closed 5 months ago

kzhui125 commented 5 months ago

Recently I try to switch chrono to time-rs. But I don't find a reliable way to get local datetime. (macOS)

Why doesn't time-rs implement local time like chrono does? including multiple thread environment.

image
#[tokio::main]
async fn main() {
    let now = chrono::Local::now();
    println!("{now}");

    let is_err = time::OffsetDateTime::now_local().is_err();
    println!("{is_err}");
}

Maybe I have to switch back to chrono to get local time.

kzhui125 commented 5 months ago

I want a local time function that won't fail...

jhpratt commented 5 months ago

Last I checked, chrono implements it in a way that is both expensive and unsound. time does what is necessary to avoid unsoundness. This has been addressed in multiple issues previously.