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

Merge pull request #2317 from deantvv/fix-touch-parse-date

Fix touch parse date error
This commit is contained in:
Sylvestre Ledru 2021-05-31 18:18:44 +02:00 committed by GitHub
commit 8d714b0ab0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 26 deletions

22
Cargo.lock generated
View file

@ -513,28 +513,6 @@ dependencies = [
"winapi 0.3.9", "winapi 0.3.9",
] ]
[[package]]
name = "csv"
version = "1.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22813a6dc45b335f9bade10bf7271dc477e81113e89eb251a0bc2a8a81c536e1"
dependencies = [
"bstr",
"csv-core",
"itoa",
"ryu",
"serde",
]
[[package]]
name = "csv-core"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90"
dependencies = [
"memchr 2.4.0",
]
[[package]] [[package]]
name = "ctor" name = "ctor"
version = "0.1.20" version = "0.1.20"

View file

@ -238,11 +238,15 @@ fn parse_date(str: &str) -> FileTime {
// be any simple specification for what format this parameter allows and I'm // be any simple specification for what format this parameter allows and I'm
// not about to implement GNU parse_datetime. // not about to implement GNU parse_datetime.
// http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob_plain;f=lib/parse-datetime.y // http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob_plain;f=lib/parse-datetime.y
match time::strptime(str, "%c") { let formats = vec!["%c", "%F"];
Ok(tm) => local_tm_to_filetime(to_local(tm)), for f in formats {
Err(e) => panic!("Unable to parse date\n{}", e), if let Ok(tm) = time::strptime(str, f) {
return local_tm_to_filetime(to_local(tm));
} }
} }
show_error!("Unable to parse date: {}\n", str);
process::exit(1);
}
fn parse_timestamp(s: &str) -> FileTime { fn parse_timestamp(s: &str) -> FileTime {
let now = time::now(); let now = time::now();

View file

@ -354,6 +354,34 @@ fn test_touch_set_date() {
assert_eq!(mtime, start_of_year); assert_eq!(mtime, start_of_year);
} }
#[test]
fn test_touch_set_date2() {
let (at, mut ucmd) = at_and_ucmd!();
let file = "test_touch_set_date";
ucmd.args(&["-d", "2000-01-23", file])
.succeeds()
.no_stderr();
assert!(at.file_exists(file));
let start_of_year = str_to_filetime("%Y%m%d%H%M", "200001230000");
let (atime, mtime) = get_file_times(&at, file);
assert_eq!(atime, mtime);
assert_eq!(atime, start_of_year);
assert_eq!(mtime, start_of_year);
}
#[test]
fn test_touch_set_date_wrong_format() {
let (_at, mut ucmd) = at_and_ucmd!();
let file = "test_touch_set_date_wrong_format";
ucmd.args(&["-d", "2005-43-21", file])
.fails()
.stderr_contains("Unable to parse date: 2005-43-21");
}
#[test] #[test]
fn test_touch_mtime_dst_succeeds() { fn test_touch_mtime_dst_succeeds() {
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();