mirror of
https://github.com/uutils/coreutils
synced 2025-01-07 10:49:09 +00:00
du: reduce the complexity by moving the time option mgmt into a function
This commit is contained in:
parent
f08b8dab23
commit
f475a36204
1 changed files with 18 additions and 14 deletions
|
@ -625,20 +625,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
|
|
||||||
if matches.contains_id(options::TIME) {
|
if matches.contains_id(options::TIME) {
|
||||||
let tm = {
|
let tm = {
|
||||||
let secs = {
|
let secs = matches
|
||||||
match matches.get_one::<String>(options::TIME) {
|
.get_one::<String>(options::TIME)
|
||||||
Some(s) => match s.as_str() {
|
.map(|s| get_time_secs(s, &stat))
|
||||||
"ctime" | "status" => stat.modified,
|
.transpose()?
|
||||||
"access" | "atime" | "use" => stat.accessed,
|
.unwrap_or(stat.modified);
|
||||||
"birth" | "creation" => stat
|
|
||||||
.created
|
|
||||||
.ok_or_else(|| DuError::InvalidTimeArg(s.into()))?,
|
|
||||||
// below should never happen as clap already restricts the values.
|
|
||||||
_ => unreachable!("Invalid field for --time"),
|
|
||||||
},
|
|
||||||
None => stat.modified,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
DateTime::<Local>::from(UNIX_EPOCH + Duration::from_secs(secs))
|
DateTime::<Local>::from(UNIX_EPOCH + Duration::from_secs(secs))
|
||||||
};
|
};
|
||||||
if !summarize || index == len - 1 {
|
if !summarize || index == len - 1 {
|
||||||
|
@ -676,6 +667,19 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_time_secs(s: &str, stat: &Stat) -> std::result::Result<u64, DuError> {
|
||||||
|
let secs = match s {
|
||||||
|
"ctime" | "status" => stat.modified,
|
||||||
|
"access" | "atime" | "use" => stat.accessed,
|
||||||
|
"birth" | "creation" => stat
|
||||||
|
.created
|
||||||
|
.ok_or_else(|| DuError::InvalidTimeArg(s.into()))?,
|
||||||
|
// below should never happen as clap already restricts the values.
|
||||||
|
_ => unreachable!("Invalid field for --time"),
|
||||||
|
};
|
||||||
|
Ok(secs)
|
||||||
|
}
|
||||||
|
|
||||||
fn parse_time_style(s: Option<&str>) -> UResult<&str> {
|
fn parse_time_style(s: Option<&str>) -> UResult<&str> {
|
||||||
match s {
|
match s {
|
||||||
Some(s) => match s {
|
Some(s) => match s {
|
||||||
|
|
Loading…
Reference in a new issue