Skip to content

Commit 37cbfd1

Browse files
authored
Merge pull request #5030 from cakebaker/touch_use_parse_datetime
touch: use parse_datetime instead of humantime_to_duration
2 parents 11b098f + e6ec149 commit 37cbfd1

File tree

4 files changed

+13
-24
lines changed

4 files changed

+13
-24
lines changed

Cargo.lock

Lines changed: 1 addition & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# coreutils (uutils)
22
# * see the repository LICENSE, README, and CONTRIBUTING files for more information
33

4-
# spell-checker:ignore (libs) libselinux gethostid procfs bigdecimal kqueue fundu mangen datetime uuhelp memmap
4+
# spell-checker:ignore (libs) bigdecimal datetime fundu gethostid kqueue libselinux mangen memmap procfs uuhelp
55

66
[package]
77
name = "coreutils"

src/uu/touch/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# spell-checker:ignore humantime
1+
# spell-checker:ignore datetime
22
[package]
33
name = "uu_touch"
44
version = "0.0.19"
@@ -18,8 +18,7 @@ path = "src/touch.rs"
1818
[dependencies]
1919
filetime = { workspace = true }
2020
clap = { workspace = true }
21-
# TODO: use workspace dependency (0.3) when switching from time to chrono
22-
humantime_to_duration = "0.2.1"
21+
parse_datetime = { workspace = true }
2322
time = { workspace = true, features = [
2423
"parsing",
2524
"formatting",

src/uu/touch/src/touch.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
8484
) {
8585
(Some(reference), Some(date)) => {
8686
let (atime, mtime) = stat(Path::new(reference), !matches.get_flag(options::NO_DEREF))?;
87-
if let Ok(offset) = humantime_to_duration::from_str(date) {
88-
let mut seconds = offset.whole_seconds();
89-
let mut nanos = offset.subsec_nanoseconds();
90-
if nanos < 0 {
91-
nanos += 1_000_000_000;
92-
seconds -= 1;
93-
}
87+
if let Ok(offset) = parse_datetime::from_str(date) {
88+
let seconds = offset.num_seconds();
89+
let nanos = offset.num_nanoseconds().unwrap_or(0) % 1_000_000_000;
9490

9591
let ref_atime_secs = atime.unix_seconds();
9692
let ref_atime_nanos = atime.nanoseconds();
@@ -445,9 +441,13 @@ fn parse_date(s: &str) -> UResult<FileTime> {
445441
}
446442
}
447443

448-
if let Ok(duration) = humantime_to_duration::from_str(s) {
444+
if let Ok(duration) = parse_datetime::from_str(s) {
449445
let now_local = time::OffsetDateTime::now_local().unwrap();
450-
let diff = now_local.checked_add(duration).unwrap();
446+
let diff = now_local
447+
.checked_add(time::Duration::nanoseconds(
448+
duration.num_nanoseconds().unwrap(),
449+
))
450+
.unwrap();
451451
return Ok(local_dt_to_filetime(diff));
452452
}
453453

0 commit comments

Comments
 (0)