-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
stat: fix precision when rendering mtime (%Y) #7115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Support precision when rendering the time of last data modification with `stat`. For example, after this commit $ stat --printf='%.1Y\n' f 1668645806.7 Previously, the precision in the format specification was ignored. This is implemented with a custom renderer because GNU `stat` seems to truncate the number as opposed to rounding the number as would happen when using `format!` with a specified number of digits of precision. Fixes uutils#3233
Sweet, i was working on it but your implementation is better than mine :) Could you please add this test? It covers more cases
|
Yes, I'll definitely add those tests, thank you. Sorry for the overlapping work.
…On Saturday, January 11th, 2025 at 4:39 AM, Sylvestre Ledru ***@***.***> wrote:
Sweet, i was working on it but your implementation is better than mine :)
Could you please add this test? It covers more cases
#[cfg(feature = "touch")]
#[test]
fn test_timestamp_format() {
let ts = TestScenario::new(util_name!());
// Create a file with a specific timestamp for testing
ts.ccmd("touch")
.args(&["-d", "1970-01-01 18:43:33.023456789", "k"])
.succeeds()
.no_stderr();
let test_cases = vec![
// Basic timestamp formats
("%Y", "67413"),
("%.Y", "67413.023456789"),
("%.1Y", "67413.0"),
("%.3Y", "67413.023"),
("%.6Y", "67413.023456"),
("%.9Y", "67413.023456789"),
// Width and padding tests
("%13.6Y", " 67413.023456"),
("%013.6Y", "067413.023456"),
("%-13.6Y", "67413.023456 "),
// Longer width/precision combinations
("%18.10Y", " 67413.0234567890"),
("%I18.10Y", " 67413.0234567890"),
("%018.10Y", "0067413.0234567890"),
("%-18.10Y", "67413.0234567890 "),
];
for (format_str, expected) in test_cases {
let result = ts
.ucmd()
.args(&["-c", format_str, "k"])
.succeeds()
.stdout_move_str();
assert_eq!(
result.trim(),
expected,
"Format '{}' failed.\nExpected: '{}'\nGot: '{}'",
format_str,
expected,
result.trim()
);
}
}
—
Reply to this email directly, [view it on GitHub](#7115 (comment)), or [unsubscribe](https://github.com/notifications/unsubscribe-auth/AAA5XG7RCQCTXITVFXXFVQ32KDRD3AVCNFSM6AAAAABU7QB25SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDKOBVGE3TOMBUGE).
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
In order to pass the new test cases, I needed to update the code a bit. There is now a There is still a few aspects of the code that are not quite right, but I think they can be fixed in separate pull requests. First, there is some numerical inaccuracy between getting the |
Yeah, it is surprisingly complex :) |
GNU testsuite comparison:
|
Support precision when rendering the time of last data modification with
stat
. For example, after this commitPreviously, the precision in the format specification was ignored. This is implemented with a custom renderer because GNU
stat
seems to truncate the number as opposed to rounding the number as would happen when usingformat!
with a specified number of digits of precision.Fixes #3233