diff --git a/src/uu/dmesg/src/dmesg.rs b/src/uu/dmesg/src/dmesg.rs index e8917e9..2ad6427 100644 --- a/src/uu/dmesg/src/dmesg.rs +++ b/src/uu/dmesg/src/dmesg.rs @@ -108,25 +108,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } if let Some(since) = matches.get_one::<String>(options::SINCE) { let since = remove_enclosing_quotes(since); - if let Ok(since) = parse_datetime::parse_datetime(since) { - dmesg.since_filter = Some(since); - } else { - return Err(USimpleError::new( - 1, - format!("invalid time value \"{since}\""), - )); - } + dmesg.since_filter = Some(time_formatter::parse_datetime(since)?); } if let Some(until) = matches.get_one::<String>(options::UNTIL) { let until = remove_enclosing_quotes(until); - if let Ok(until) = parse_datetime::parse_datetime(until) { - dmesg.until_filter = Some(until); - } else { - return Err(USimpleError::new( - 1, - format!("invalid time value \"{until}\""), - )); - } + dmesg.until_filter = Some(time_formatter::parse_datetime(until)?); } dmesg.print()?; Ok(()) diff --git a/src/uu/dmesg/src/time_formatter.rs b/src/uu/dmesg/src/time_formatter.rs index 24a0be1..b180d0e 100644 --- a/src/uu/dmesg/src/time_formatter.rs +++ b/src/uu/dmesg/src/time_formatter.rs @@ -7,6 +7,7 @@ use chrono::{DateTime, FixedOffset, TimeDelta}; #[cfg(feature = "fixed-boot-time")] use chrono::{NaiveDate, NaiveTime}; use std::sync::OnceLock; +use uucore::error::{UResult, USimpleError}; pub fn raw(timestamp_us: i64) -> String { let seconds = timestamp_us / 1000000; @@ -116,6 +117,15 @@ impl DeltaFormatter { } } +pub fn parse_datetime(s: &str) -> UResult<DateTime<FixedOffset>> { + #[cfg(feature = "fixed-boot-time")] + set_fixed_timezone(); + match parse_datetime::parse_datetime(s) { + Ok(date_time) => Ok(date_time), + Err(_) => Err(USimpleError::new(1, format!("invalid time value \"{s}\""))), + } +} + pub fn datetime_from_microseconds_since_boot(microseconds: i64) -> DateTime<FixedOffset> { boot_time() .checked_add_signed(TimeDelta::microseconds(microseconds)) @@ -174,3 +184,11 @@ fn boot_time_from_utmpx() -> Option<DateTime<FixedOffset>> { } None } + +#[cfg(feature = "fixed-boot-time")] +static SET_TZ: OnceLock<()> = OnceLock::new(); + +#[cfg(feature = "fixed-boot-time")] +fn set_fixed_timezone() { + *SET_TZ.get_or_init(|| std::env::set_var("TZ", "Asia/Jakarta")) +}