Skip to content

Commit 9c13e3f

Browse files
committed
stat: Use uucore::time:format_system_time function
1 parent fb4d611 commit 9c13e3f

2 files changed

Lines changed: 22 additions & 17 deletions

File tree

src/uu/stat/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ path = "src/stat.rs"
1919

2020
[dependencies]
2121
clap = { workspace = true }
22-
uucore = { workspace = true, features = ["entries", "libc", "fs", "fsext"] }
22+
uucore = { workspace = true, features = [
23+
"entries",
24+
"libc",
25+
"fs",
26+
"fsext",
27+
"time",
28+
] }
2329
chrono = { workspace = true }
2430
thiserror = { workspace = true }
2531

src/uu/stat/src/stat.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use clap::builder::ValueParser;
1010
use uucore::display::Quotable;
1111
use uucore::fs::display_permissions;
1212
use uucore::fsext::{
13-
BirthTime, FsMeta, StatFs, pretty_filetype, pretty_fstype, read_fs_list, statfs,
13+
BirthTime, FsMeta, MetadataTimeField, StatFs, metadata_get_time, pretty_filetype,
14+
pretty_fstype, read_fs_list, statfs,
1415
};
1516
use uucore::libc::mode_t;
1617
use uucore::{entries, format_usage, show_error, show_warning};
@@ -1026,21 +1027,17 @@ impl Stater {
10261027
}
10271028

10281029
// time of file birth, human-readable; - if unknown
1029-
'w' => {
1030-
OutputType::Str(meta.birth().map_or(String::from("-"), |(sec, nsec)| {
1031-
pretty_time(sec as i64, nsec as i64)
1032-
}))
1033-
}
1030+
'w' => OutputType::Str(pretty_time(meta, MetadataTimeField::Birth)),
10341031

10351032
// time of file birth, seconds since Epoch; 0 if unknown
10361033
'W' => OutputType::Unsigned(meta.birth().unwrap_or_default().0),
10371034

10381035
// time of last access, human-readable
1039-
'x' => OutputType::Str(pretty_time(meta.atime(), meta.atime_nsec())),
1036+
'x' => OutputType::Str(pretty_time(meta, MetadataTimeField::Access)),
10401037
// time of last access, seconds since Epoch
10411038
'X' => OutputType::Integer(meta.atime()),
10421039
// time of last data modification, human-readable
1043-
'y' => OutputType::Str(pretty_time(meta.mtime(), meta.mtime_nsec())),
1040+
'y' => OutputType::Str(pretty_time(meta, MetadataTimeField::Modification)),
10441041
// time of last data modification, seconds since Epoch
10451042
'Y' => {
10461043
let sec = meta.mtime();
@@ -1060,7 +1057,7 @@ impl Stater {
10601057
}
10611058
}
10621059
// time of last status change, human-readable
1063-
'z' => OutputType::Str(pretty_time(meta.ctime(), meta.ctime_nsec())),
1060+
'z' => OutputType::Str(pretty_time(meta, MetadataTimeField::Change)),
10641061
// time of last status change, seconds since Epoch
10651062
'Z' => OutputType::Integer(meta.ctime()),
10661063
'R' => {
@@ -1295,14 +1292,16 @@ pub fn uu_app() -> Command {
12951292
)
12961293
}
12971294

1298-
const PRETTY_DATETIME_FORMAT: &str = "%Y-%m-%d %H:%M:%S.%f %z";
1299-
1300-
fn pretty_time(sec: i64, nsec: i64) -> String {
1301-
// Return the date in UTC
1302-
let tm = DateTime::from_timestamp(sec, nsec as u32).unwrap_or_default();
1303-
let tm: DateTime<Local> = tm.into();
1295+
const PRETTY_DATETIME_FORMAT: &str = "%Y-%m-%d %H:%M:%S.%N %z";
13041296

1305-
tm.format(PRETTY_DATETIME_FORMAT).to_string()
1297+
fn pretty_time(meta: &Metadata, md_time_field: MetadataTimeField) -> String {
1298+
if let Some(time) = metadata_get_time(meta, md_time_field) {
1299+
let mut tmp = Vec::new();
1300+
if uucore::time::format_system_time(&mut tmp, time, PRETTY_DATETIME_FORMAT, false).is_ok() {
1301+
return String::from_utf8(tmp).unwrap();
1302+
}
1303+
}
1304+
"-".to_string()
13061305
}
13071306

13081307
#[cfg(test)]

0 commit comments

Comments
 (0)