Skip to content

Commit 6ea955d

Browse files
committed
du: support arbitrary time formats
1 parent 181885b commit 6ea955d

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/uu/du/src/du.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
708708
};
709709

710710
let time_format = if time.is_some() {
711-
parse_time_style(matches.get_one::<String>("time-style").map(|s| s.as_str()))?.to_string()
711+
parse_time_style(
712+
matches
713+
.get_one::<String>(options::TIME_STYLE)
714+
.map(|s| s.as_str()),
715+
)?
716+
.to_string()
712717
} else {
713718
"%Y-%m-%d %H:%M".to_string()
714719
};
@@ -807,7 +812,10 @@ fn parse_time_style(s: Option<&str>) -> UResult<&str> {
807812
"full-iso" => Ok("%Y-%m-%d %H:%M:%S.%f %z"),
808813
"long-iso" => Ok("%Y-%m-%d %H:%M"),
809814
"iso" => Ok("%Y-%m-%d"),
810-
_ => Err(DuError::InvalidTimeStyleArg(s.into()).into()),
815+
_ => match s.strip_prefix('+') {
816+
Some(s) => Ok(s),
817+
_ => Err(DuError::InvalidTimeStyleArg(s.into()).into()),
818+
},
811819
},
812820
None => Ok("%Y-%m-%d %H:%M"),
813821
}

tests/by-util/test_du.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#[cfg(not(windows))]
88
use regex::Regex;
99

10+
use rstest::rstest;
1011
use uutests::at_and_ucmd;
1112
use uutests::new_ucmd;
1213
#[cfg(not(target_os = "windows"))]
@@ -1173,6 +1174,17 @@ fn test_invalid_time_style() {
11731174
.stdout_does_not_contain("du: invalid argument 'banana' for 'time style'");
11741175
}
11751176

1177+
#[rstest]
1178+
#[case::full_iso("+%Y-%m-%d %H:%M:%S.%f %z")]
1179+
#[case::long_iso("+%Y-%m-%d %H:%M")]
1180+
#[case::iso("+%Y-%m-%d")]
1181+
#[case::seconds("+%S")]
1182+
fn test_valid_time_style(#[case] input: &str) {
1183+
new_ucmd!()
1184+
.args(&["--time", "--time-style", input])
1185+
.succeeds();
1186+
}
1187+
11761188
#[test]
11771189
fn test_human_size() {
11781190
use std::fs::File;

0 commit comments

Comments
 (0)