From b70f29609bb1baefb45b2549e2ebb79bda08f11f Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Sun, 28 Jul 2024 18:20:56 -0400 Subject: [PATCH] local-time: switch from `time` to `jiff` This swaps out `time` in favor of `jiff` for getting and formatting the local time. Note that this does add the `%Z` to the format string, which will write out time zone abbreviations like `EDT` along with the local datetime itself. The `time` crate doesn't support this, but jiff's tzdb integration let's it do it. --- Cargo.toml | 4 ++-- src/lib.rs | 2 +- src/time.rs | 15 +++++++-------- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3e01702..23fc795 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,7 +54,7 @@ render-line = ["crosstermion/color", "humantime", "unicode-width"] render-line-crossterm = ["crosstermion/crossterm"] render-line-autoconfigure = ["is-terminal"] -local-time = ["time"] +local-time = ["jiff"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] @@ -76,7 +76,7 @@ crosstermion = { version = "0.14.0", optional = true, default-features = false } async-io = { version = "2.2.1", optional = true } # localtime support for render-tui -time = { version = "0.3.2", optional = true, features = ["std", "local-offset", "formatting"], default-features = false } +jiff = { version = "0.1.1", optional = true } # line renderer ctrlc = { version = "3.1.4", optional = true, default-features = false, features = ['termination'] } diff --git a/src/lib.rs b/src/lib.rs index 8b35afc..9010569 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,7 +49,7 @@ pub use log::info; #[cfg(feature = "progress-tree-log")] pub use log::warn; -#[cfg(any(feature = "humantime", feature = "time"))] +#[cfg(any(feature = "humantime", feature = "local-time"))] /// pub mod time; diff --git a/src/time.rs b/src/time.rs index 43d9af1..88d6c0b 100644 --- a/src/time.rs +++ b/src/time.rs @@ -2,24 +2,23 @@ mod localtime { use std::time::SystemTime; + use jiff::Zoned; + /// Return a string representing the current date and time as localtime. /// /// Available with the `localtime` feature toggle. pub fn format_now_datetime_seconds() -> String { - let t = time::OffsetDateTime::now_utc(); - t.to_offset(time::UtcOffset::local_offset_at(t).unwrap_or(time::UtcOffset::UTC)) - .format(&time::format_description::parse("%F %T").expect("format known to work")) - .expect("formatting always works") + Zoned::now().strftime("%F %T %Z").to_string() } /// Return a string representing the current time as localtime. /// /// Available with the `localtime` feature toggle. pub fn format_time_for_messages(time: SystemTime) -> String { - time::OffsetDateTime::from(time) - .to_offset(time::UtcOffset::current_local_offset().unwrap_or(time::UtcOffset::UTC)) - .format(&time::format_description::parse("[hour]:[minute]:[second]").expect("format known to work")) - .expect("formatting always works") + Zoned::try_from(time) + .expect("system time is always in range -9999-01-01..=9999-12-31") + .strftime("%T") + .to_string() } }