Skip to content
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

Does tz-rs support bridge(integrate) the time crate. #31

Closed
baoyachi opened this issue Jul 13, 2022 · 1 comment
Closed

Does tz-rs support bridge(integrate) the time crate. #31

baoyachi opened this issue Jul 13, 2022 · 1 comment

Comments

@baoyachi
Copy link

Now, I want use time now_local method in time crate. but On POSIX systems, the method would be unsound if it returned a success value.

Does tz-rs support any method bridge(integrate) the time crate?

@x-hgg-x
Copy link
Owner

x-hgg-x commented Jul 13, 2022

There is currently no integration with the time crate, but you can do something like this:

use std::time::{SystemTime, UNIX_EPOCH};
use tz::TimeZone;
use time::{OffsetDateTime, UtcOffset};

let time_zone_local = TimeZone::local()?; // expensive call, should be cached

let duration_since_epoch = SystemTime::now().duration_since(UNIX_EPOCH)?;
let local_time_type = time_zone_local.find_local_time_type(duration_since_epoch.as_secs().try_into()?)?;
let time_zone_offset = UtcOffset::from_whole_seconds(local_time_type.ut_offset())?;
let local_date_time = (OffsetDateTime::UNIX_EPOCH + duration_since_epoch).to_offset(time_zone_offset);

@x-hgg-x x-hgg-x closed this as completed Jul 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants