diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 7f4ac1a16..bbf90a711 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -28,7 +28,7 @@ use uucore::translate; use uucore::parser::parse_glob; use uucore::parser::parse_size::{ParseSizeError, parse_size_u64}; use uucore::parser::shortcut_value_parser::ShortcutValueParser; -use uucore::time::{FormatSystemTimeFallback, format_system_time}; +use uucore::time::{FormatSystemTimeFallback, format, format_system_time}; use uucore::{format_usage, show, show_error, show_warning}; #[cfg(windows)] use windows_sys::Win32::Foundation::HANDLE; @@ -668,7 +668,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let time_format = if time.is_some() { parse_time_style(matches.get_one::("time-style").map(|s| s.as_str()))?.to_string() } else { - "%Y-%m-%d %H:%M".to_string() + format::LONG_ISO.to_string() }; let stat_printer = StatPrinter { @@ -758,15 +758,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { fn parse_time_style(s: Option<&str>) -> UResult<&str> { match s { Some(s) => match s { - "full-iso" => Ok("%Y-%m-%d %H:%M:%S.%f %z"), - "long-iso" => Ok("%Y-%m-%d %H:%M"), - "iso" => Ok("%Y-%m-%d"), + "full-iso" => Ok(format::FULL_ISO), + "long-iso" => Ok(format::LONG_ISO), + "iso" => Ok(format::ISO), _ => match s.chars().next().unwrap() { '+' => Ok(&s[1..]), _ => Err(DuError::InvalidTimeStyleArg(s.into()).into()), }, }, - None => Ok("%Y-%m-%d %H:%M"), + None => Ok(format::LONG_ISO), } } diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index e36a48494..20f6ba66d 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -60,7 +60,7 @@ use uucore::line_ending::LineEnding; use uucore::translate; use uucore::quoting_style::{QuotingStyle, locale_aware_escape_dir_name, locale_aware_escape_name}; -use uucore::time::{FormatSystemTimeFallback, format_system_time}; +use uucore::time::{FormatSystemTimeFallback, format, format_system_time}; use uucore::{ display::Quotable, error::{UError, UResult, set_exit_code}, @@ -251,16 +251,8 @@ enum Files { } fn parse_time_style(options: &clap::ArgMatches) -> Result<(String, Option), LsError> { - const TIME_STYLES: [(&str, (&str, Option<&str>)); 4] = [ - ("full-iso", ("%Y-%m-%d %H:%M:%S.%f %z", None)), - ("long-iso", ("%Y-%m-%d %H:%M", None)), - ("iso", ("%m-%d %H:%M", Some("%Y-%m-%d "))), - // TODO: Using correct locale string is not implemented. - ("locale", ("%b %e %H:%M", Some("%b %e %Y"))), - ]; - // A map from a time-style parameter to a length-2 tuple of formats: - // the first one is used for recent dates, the second one for older ones (optional). - let time_styles = HashMap::from(TIME_STYLES); + // TODO: Using correct locale string is not implemented. + const LOCALE_FORMAT: (&str, Option<&str>) = ("%b %e %H:%M", Some("%b %e %Y")); // Convert time_styles references to owned String/option. fn ok((recent, older): (&str, Option<&str>)) -> Result<(String, Option), LsError> { @@ -278,7 +270,7 @@ fn parse_time_style(options: &clap::ArgMatches) -> Result<(String, Option options.indices_of(options::TIME_STYLE).unwrap().next_back() { - ok(time_styles["full-iso"]) + ok((format::FULL_ISO, None)) } else { let field = if let Some(field) = field.strip_prefix("posix-") { // See GNU documentation, set format to "locale" if LC_TIME="POSIX", @@ -288,16 +280,23 @@ fn parse_time_style(options: &clap::ArgMatches) -> Result<(String, Option ok(*formats), - None => match field.chars().next().unwrap() { + match field { + "full-iso" => ok((format::FULL_ISO, None)), + "long-iso" => ok((format::LONG_ISO, None)), + // ISO older format needs extra padding. + "iso" => Ok(( + "%m-%d %H:%M".to_string(), + Some(format::ISO.to_string() + " "), + )), + "locale" => ok(LOCALE_FORMAT), + _ => match field.chars().next().unwrap() { '+' => { // recent/older formats are (optionally) separated by a newline let mut it = field[1..].split('\n'); @@ -313,9 +312,9 @@ fn parse_time_style(options: &clap::ArgMatches) -> Result<(String, Option (i64, u32) { } } +pub mod format { + pub static FULL_ISO: &str = "%Y-%m-%d %H:%M:%S.%N %z"; + pub static LONG_ISO: &str = "%Y-%m-%d %H:%M"; + pub static ISO: &str = "%Y-%m-%d"; +} + /// Sets how `format_system_time` behaves if the time cannot be converted. pub enum FormatSystemTimeFallback { Integer, // Just print seconds since epoch (`ls`)