Skip to content

Commit

Permalink
local-time: switch from time to jiff
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
BurntSushi committed Jul 28, 2024
1 parent 10800f3 commit b70f296
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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'] }
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
15 changes: 7 additions & 8 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down

0 comments on commit b70f296

Please sign in to comment.