dmesg: set to fixed timezone in test.

This commit is contained in:
Fuad Ismail 2024-12-11 15:48:51 +07:00
parent a35c00b0e4
commit 48c4f78088
2 changed files with 20 additions and 16 deletions

@ -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(())

@ -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"))
}