Skip to content

Commit b99615c

Browse files
committed
du/ls: Move common time formats to constants in uucore/time
Along the way: - Fix the full-iso format, that was supposed to use %N, not %f (%f padded with chrono, but not with jiff, and %N is more correct and what GNU says in their manual) - The Hashmap thing in parse_time_style was too smart, to a point that it became too unflexible (it would have been even worse when we added locale support). I was hoping the share more of the code, but that seems difficult.
1 parent 1272bfc commit b99615c

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

src/uu/du/src/du.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use uucore::translate;
2828
use uucore::parser::parse_glob;
2929
use uucore::parser::parse_size::{ParseSizeError, parse_size_u64};
3030
use uucore::parser::shortcut_value_parser::ShortcutValueParser;
31-
use uucore::time::{FormatSystemTimeFallback, format_system_time};
31+
use uucore::time::{FormatSystemTimeFallback, format, format_system_time};
3232
use uucore::{format_usage, show, show_error, show_warning};
3333
#[cfg(windows)]
3434
use windows_sys::Win32::Foundation::HANDLE;
@@ -668,7 +668,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
668668
let time_format = if time.is_some() {
669669
parse_time_style(matches.get_one::<String>("time-style").map(|s| s.as_str()))?.to_string()
670670
} else {
671-
"%Y-%m-%d %H:%M".to_string()
671+
format::LONG_ISO.to_string()
672672
};
673673

674674
let stat_printer = StatPrinter {
@@ -758,15 +758,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
758758
fn parse_time_style(s: Option<&str>) -> UResult<&str> {
759759
match s {
760760
Some(s) => match s {
761-
"full-iso" => Ok("%Y-%m-%d %H:%M:%S.%f %z"),
762-
"long-iso" => Ok("%Y-%m-%d %H:%M"),
763-
"iso" => Ok("%Y-%m-%d"),
761+
"full-iso" => Ok(format::FULL_ISO),
762+
"long-iso" => Ok(format::LONG_ISO),
763+
"iso" => Ok(format::ISO),
764764
_ => match s.chars().next().unwrap() {
765765
'+' => Ok(&s[1..]),
766766
_ => Err(DuError::InvalidTimeStyleArg(s.into()).into()),
767767
},
768768
},
769-
None => Ok("%Y-%m-%d %H:%M"),
769+
None => Ok(format::LONG_ISO),
770770
}
771771
}
772772

src/uu/ls/src/ls.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use uucore::line_ending::LineEnding;
6060
use uucore::translate;
6161

6262
use uucore::quoting_style::{QuotingStyle, locale_aware_escape_dir_name, locale_aware_escape_name};
63-
use uucore::time::{FormatSystemTimeFallback, format_system_time};
63+
use uucore::time::{FormatSystemTimeFallback, format, format_system_time};
6464
use uucore::{
6565
display::Quotable,
6666
error::{UError, UResult, set_exit_code},
@@ -251,16 +251,8 @@ enum Files {
251251
}
252252

253253
fn parse_time_style(options: &clap::ArgMatches) -> Result<(String, Option<String>), LsError> {
254-
const TIME_STYLES: [(&str, (&str, Option<&str>)); 4] = [
255-
("full-iso", ("%Y-%m-%d %H:%M:%S.%f %z", None)),
256-
("long-iso", ("%Y-%m-%d %H:%M", None)),
257-
("iso", ("%m-%d %H:%M", Some("%Y-%m-%d "))),
258-
// TODO: Using correct locale string is not implemented.
259-
("locale", ("%b %e %H:%M", Some("%b %e %Y"))),
260-
];
261-
// A map from a time-style parameter to a length-2 tuple of formats:
262-
// the first one is used for recent dates, the second one for older ones (optional).
263-
let time_styles = HashMap::from(TIME_STYLES);
254+
// TODO: Using correct locale string is not implemented.
255+
const LOCALE_FORMAT: (&str, Option<&str>) = ("%b %e %H:%M", Some("%b %e %Y"));
264256

265257
// Convert time_styles references to owned String/option.
266258
fn ok((recent, older): (&str, Option<&str>)) -> Result<(String, Option<String>), LsError> {
@@ -278,7 +270,7 @@ fn parse_time_style(options: &clap::ArgMatches) -> Result<(String, Option<String
278270
&& options.indices_of(options::FULL_TIME).unwrap().next_back()
279271
> options.indices_of(options::TIME_STYLE).unwrap().next_back()
280272
{
281-
ok(time_styles["full-iso"])
273+
ok((format::FULL_ISO, None))
282274
} else {
283275
let field = if let Some(field) = field.strip_prefix("posix-") {
284276
// 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<String
288280
if std::env::var("LC_TIME").unwrap_or_default() == "POSIX"
289281
|| std::env::var("LC_ALL").unwrap_or_default() == "POSIX"
290282
{
291-
return ok(time_styles["locale"]);
283+
return ok(LOCALE_FORMAT);
292284
}
293285
field
294286
} else {
295287
&field
296288
};
297289

298-
match time_styles.get(field) {
299-
Some(formats) => ok(*formats),
300-
None => match field.chars().next().unwrap() {
290+
match field {
291+
"full-iso" => ok((format::FULL_ISO, None)),
292+
"long-iso" => ok((format::LONG_ISO, None)),
293+
// ISO older format needs extra padding.
294+
"iso" => Ok((
295+
"%m-%d %H:%M".to_string(),
296+
Some(format::ISO.to_string() + " "),
297+
)),
298+
"locale" => ok(LOCALE_FORMAT),
299+
_ => match field.chars().next().unwrap() {
301300
'+' => {
302301
// recent/older formats are (optionally) separated by a newline
303302
let mut it = field[1..].split('\n');
@@ -313,9 +312,9 @@ fn parse_time_style(options: &clap::ArgMatches) -> Result<(String, Option<String
313312
}
314313
}
315314
} else if options.get_flag(options::FULL_TIME) {
316-
ok(time_styles["full-iso"])
315+
ok((format::FULL_ISO, None))
317316
} else {
318-
ok(time_styles["locale"])
317+
ok(LOCALE_FORMAT)
319318
}
320319
}
321320

src/uucore/src/lib/features/time.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ pub fn system_time_to_sec(time: SystemTime) -> (i64, u32) {
3636
}
3737
}
3838

39+
pub mod format {
40+
pub static FULL_ISO: &str = "%Y-%m-%d %H:%M:%S.%N %z";
41+
pub static LONG_ISO: &str = "%Y-%m-%d %H:%M";
42+
pub static ISO: &str = "%Y-%m-%d";
43+
}
44+
3945
/// Sets how `format_system_time` behaves if the time cannot be converted.
4046
pub enum FormatSystemTimeFallback {
4147
Integer, // Just print seconds since epoch (`ls`)

0 commit comments

Comments
 (0)