1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Fix touch -t with 2 digit years when YY > 68

When using `touch -t` with a 2 digit year, the year is interpreted as
a relative year to 2000.

When the year is 68 or less, it should be interpreted as 20xx.
When the year is 69 or more, it should be interpreted as 19xx.

This is the behavior of GNU `touch`.

fixes gh-7280

Arguably 2 digits years should be deprecated as we
are already closer to 2069, than 1969.
This commit is contained in:
M Bussonnier 2025-03-03 16:52:53 +01:00 committed by Dorian Péron
parent d570512bdc
commit 7632acfc90
2 changed files with 66 additions and 3 deletions

View file

@ -118,6 +118,45 @@ fn test_touch_set_mdhms_time() {
assert_eq!(mtime.unix_seconds() - start_of_year.unix_seconds(), 45296);
}
#[test]
fn test_touch_2_digit_years_68() {
// 68 and before are 20xx
let (at, mut ucmd) = at_and_ucmd!();
let file = "test_touch_set_two_digit_68_time";
ucmd.args(&["-t", "6801010000", file])
.succeeds()
.no_output();
assert!(at.file_exists(file));
// January 1, 2068, 00:00:00
let expected = FileTime::from_unix_time(3_092_601_600, 0);
let (atime, mtime) = get_file_times(&at, file);
assert_eq!(atime, mtime);
assert_eq!(atime, expected);
assert_eq!(mtime, expected);
}
#[test]
fn test_touch_2_digit_years_69() {
// 69 and after are 19xx
let (at, mut ucmd) = at_and_ucmd!();
let file = "test_touch_set_two_digit_69_time";
ucmd.args(&["-t", "6901010000", file])
.succeeds()
.no_output();
assert!(at.file_exists(file));
// January 1, 1969, 00:00:00
let expected = FileTime::from_unix_time(-31_536_000, 0);
let (atime, mtime) = get_file_times(&at, file);
assert_eq!(atime, mtime);
assert_eq!(atime, expected);
assert_eq!(mtime, expected);
}
#[test]
fn test_touch_set_ymdhm_time() {
let (at, mut ucmd) = at_and_ucmd!();