Skip to content

Commit

Permalink
Use tzdb to support Windows hosts, too
Browse files Browse the repository at this point in the history
tz-rs's `TimeZone::local()` is only supported for Linux hosts, but will
fail e.g. on Windows. Shadow-rs will fallback to use UTC dates, so it
will still be usable on Window.

tzdb's `local_tz()` bundles the Timezone Database, and works on Linux,
Windows, MacOS, FreeBSD, and NetBSD. While having local datetimes on
these host platforms is probably not particularly important, I still
think it's a better user experience.

tzdb itself uses tz-rs to make use of the timezone data.
  • Loading branch information
Kijewski committed Jul 21, 2022
1 parent 1b65c24 commit f1d01f2
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ all-features = true
[dependencies]
is_debug = "1.0.1"
const_format = "0.2.22"
tz-rs = "0.6.11"
time = { version = "0.3.11", features = ["formatting", "local-offset", "parsing"] }
tzdb = { version = "0.2.7", default-features = false, features = ["local"] }

#! Optional Dependencies:

Expand Down
4 changes: 2 additions & 2 deletions src/data_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::time::{SystemTime, UNIX_EPOCH};
use time::format_description::well_known::{Rfc2822, Rfc3339};
use time::UtcOffset;
use time::{format_description, OffsetDateTime};
use tz::TimeZone;

pub enum DateTime {
Local(OffsetDateTime),
Expand Down Expand Up @@ -50,7 +49,8 @@ impl DateTime {
}

pub fn local_now() -> Result<Self, Box<dyn Error>> {
let time_zone_local = TimeZone::local()?; // expensive call, should be cached
// expensive call, should be cached
let time_zone_local = tzdb::local_tz().ok_or("Could not get local timezone")?;

let duration_since_epoch = SystemTime::now().duration_since(UNIX_EPOCH)?;
let local_time_type =
Expand Down

0 comments on commit f1d01f2

Please sign in to comment.