Skip to content

Commit 2481f9a

Browse files
committed
Always use UTC for formatting timestamps in unit tests.
1 parent 4d8a54e commit 2481f9a

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

src/main.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use anyhow::{Context, Result};
88
#[cfg(feature = "dbus")]
99
use async_io::block_on;
10-
use chrono::{Local, TimeZone};
10+
use chrono::TimeZone;
1111
use clap::{Parser, Subcommand, ValueEnum};
1212
use std::fs;
1313
use std::path::PathBuf;
@@ -354,9 +354,17 @@ fn format_active_flags(be: &BootEnvironment) -> Option<String> {
354354
}
355355

356356
fn format_timestamp(timestamp: i64) -> String {
357-
match Local.timestamp_opt(timestamp, 0) {
358-
chrono::LocalResult::Single(dt) => dt.format("%Y-%m-%d %H:%M").to_string(),
359-
_ => format!("{}", timestamp), // Fallback to raw timestamp if conversion fails
357+
// Always use UTC for unit tests.
358+
if cfg!(test) {
359+
match chrono::Utc.timestamp_opt(timestamp, 0) {
360+
chrono::LocalResult::Single(dt) => dt.format("%Y-%m-%d %H:%M").to_string(),
361+
_ => format!("{}", timestamp),
362+
}
363+
} else {
364+
match chrono::Local.timestamp_opt(timestamp, 0) {
365+
chrono::LocalResult::Single(dt) => dt.format("%Y-%m-%d %H:%M").to_string(),
366+
_ => format!("{}", timestamp),
367+
}
360368
}
361369
}
362370

@@ -801,8 +809,8 @@ mod tests {
801809
assert_eq!(
802810
String::from_utf8(output).unwrap(),
803811
r"NAME ACTIVE MOUNTPOINT SPACE CREATED DESCRIPTION
804-
default NR / 906M 2021-06-10 01:09 -
805-
alt - - 8K 2021-06-10 02:11 Testing
812+
default NR / 906M 2021-06-10 05:09 -
813+
alt - - 8K 2021-06-10 06:11 Testing
806814
"
807815
);
808816
}
@@ -1056,11 +1064,11 @@ alt - - 8K 2021-06-10 02:11 Testing
10561064
assert_eq!(
10571065
String::from_utf8(output).unwrap(),
10581066
r"NAME ACTIVE MOUNTPOINT SPACE CREATED DESCRIPTION
1059-
default NR / 906M 2021-06-10 01:09 -
1060-
default@2021-06-10-04:30 - - 395K 2021-06-10 01:30 Automatic snapshot
1061-
default@2021-06-10-05:10 - - 395K 2021-06-10 02:10 -
1062-
alt - - 8K 2021-06-10 02:11 Testing
1063-
alt@backup - - 1K 2021-06-10 02:20 Manual backup
1067+
default NR / 906M 2021-06-10 05:09 -
1068+
default@2021-06-10-04:30 - - 395K 2021-06-10 05:30 Automatic snapshot
1069+
default@2021-06-10-05:10 - - 395K 2021-06-10 06:10 -
1070+
alt - - 8K 2021-06-10 06:11 Testing
1071+
alt@backup - - 1K 2021-06-10 06:20 Manual backup
10641072
"
10651073
);
10661074

0 commit comments

Comments
 (0)