Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 66 additions & 4 deletions src/uu/date/src/format_modifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,40 @@ fn apply_modifiers(value: &str, parsed: &ParsedSpec<'_>) -> Result<String, Forma
let flags = parsed.flags;
let width = parsed.width;
let specifier = parsed.spec;

// %N: nanoseconds have unique semantics
if specifier.ends_with('N') {
let no_pad = flags.contains('-');
let underscore = flags.contains('_');

// - without width: no-op, return full 9 digits
if no_pad && width.is_none() {
return Ok(value.to_string());
}

let target_width = match width {
Some(w) if w > 9 => {
return Err(FormatError::FieldWidthTooLarge {
width: w,
specifier: specifier.to_string(),
});
}
Some(w) => w,
None => 9,
};

let truncated = &value[..target_width.min(value.len())];

if underscore {
let trimmed = truncated.trim_end_matches('0');
let core = if trimmed.is_empty() { "0" } else { trimmed };
let padding = target_width.saturating_sub(core.len());
return Ok(format!("{core}{}", " ".repeat(padding)));
}

return Ok(truncated.to_string());
}

let mut result = value.to_string();

// Determine default pad character based on specifier type
Expand Down Expand Up @@ -474,10 +508,6 @@ fn apply_modifiers(value: &str, parsed: &ParsedSpec<'_>) -> Result<String, Forma
padded.push_str(&result);
result = padded;
}
} else if specifier.ends_with('N') {
if effective_width <= get_default_width(specifier) && effective_width != 0 {
result.truncate(effective_width);
}
}

Ok(result)
Expand Down Expand Up @@ -1027,4 +1057,36 @@ mod tests {
assert_eq!(has_gnu_modifiers(input), *expected, "input = {input:?}");
}
}

#[test]
fn test_apply_modifiers_nanoseconds() {
assert_eq!(
apply_modifiers("123456789", &spec("", None, "N")).unwrap(),
"123456789"
);
assert_eq!(
apply_modifiers("000000000", &spec("", None, "N")).unwrap(),
"000000000"
);
assert_eq!(
apply_modifiers("123456789", &spec("", Some(3), "N")).unwrap(),
"123"
);
assert_eq!(
apply_modifiers("000000000", &spec("_", Some(3), "N")).unwrap(),
"0 "
);
assert_eq!(
apply_modifiers("000000000", &spec("-", None, "N")).unwrap(),
"000000000"
);
assert_eq!(
apply_modifiers("000000000", &spec("-", Some(3), "N")).unwrap(),
"000"
);
let err = apply_modifiers("123456789", &spec("", Some(10), "N")).unwrap_err();
assert!(
matches!(err, FormatError::FieldWidthTooLarge { width, specifier } if width == 10 && specifier == "N")
);
}
}
33 changes: 33 additions & 0 deletions tests/by-util/test_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,39 @@ fn test_date_nano_seconds() {
new_ucmd!().arg("+%N").succeeds().stdout_matches(&re);
}

#[test]
fn test_date_nano_seconds_modifiers() {
let scene = TestScenario::new(util_name!());

// (epoch_input, format, expected_output)
let cases = [
("@0.123456789", "%N", "123456789"),
("@0.123456789", "%3N", "123"),
("@0.123456789", "%6N", "123456"),
("@0.120000000", "%_3N", "12 "),
("@0.100000000", "%_3N", "1 "),
("@0.000000000", "%_3N", "0 "),
("@0.123000000", "%_6N", "123 "),
("@0.123456789", "%-N", "123456789"),
("@0.123456789", "%-3N", "123"),
];

for (input, fmt, expected) in cases {
scene
.ucmd()
.args(&["-d", input, &format!("+{fmt}")])
.succeeds()
.stdout_is(format!("{expected}\n"));
}

// width > 9 is invalid
scene
.ucmd()
.arg("+%10N")
.fails()
.stderr_contains("is too large for specifier");
}

#[test]
fn test_date_format_without_plus() {
// [+FORMAT]
Expand Down
Loading