Skip to content

Commit

Permalink
touch: fix deprecation warnings from chrono
Browse files Browse the repository at this point in the history
datetime_from_str() has been deprecated
  • Loading branch information
cakebaker committed Sep 12, 2023
1 parent e131ecd commit 6ce8075
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
27 changes: 19 additions & 8 deletions src/uu/touch/src/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// spell-checker:ignore (ToDO) filetime datetime lpszfilepath mktime DATETIME subsecond datelike timelike
// spell-checker:ignore (ToDO) filetime datetime lpszfilepath mktime DATETIME datelike timelike
// spell-checker:ignore (FORMATS) MMDDhhmm YYYYMMDDHHMM YYMMDDHHMM YYYYMMDDHHMMS

use chrono::{DateTime, Datelike, Duration, Local, NaiveDate, NaiveTime, TimeZone, Timelike, Utc};
use chrono::{
DateTime, Datelike, Duration, Local, LocalResult, NaiveDate, NaiveDateTime, NaiveTime,
TimeZone, Timelike,
};
use clap::builder::ValueParser;
use clap::{crate_version, Arg, ArgAction, ArgGroup, Command};
use filetime::{set_file_times, set_symlink_file_times, FileTime};
Expand Down Expand Up @@ -348,8 +351,8 @@ fn parse_date(s: &str) -> UResult<FileTime> {
// Tue Dec 3 ...
// ("%c", POSIX_LOCALE_FORMAT),
//
if let Ok(parsed) = Local.datetime_from_str(s, format::POSIX_LOCALE) {
return Ok(datetime_to_filetime(&parsed));
if let Ok(parsed) = NaiveDateTime::parse_from_str(s, format::POSIX_LOCALE) {
return Ok(datetime_to_filetime(&parsed.and_utc()));
}

// Also support other formats found in the GNU tests like
Expand All @@ -361,8 +364,8 @@ fn parse_date(s: &str) -> UResult<FileTime> {
format::YYYY_MM_DD_HH_MM,
format::YYYYMMDDHHMM_OFFSET,
] {
if let Ok(parsed) = Utc.datetime_from_str(s, fmt) {
return Ok(datetime_to_filetime(&parsed));
if let Ok(parsed) = NaiveDateTime::parse_from_str(s, fmt) {
return Ok(datetime_to_filetime(&parsed.and_utc()));
}
}

Expand Down Expand Up @@ -411,9 +414,17 @@ fn parse_timestamp(s: &str) -> UResult<FileTime> {
}
};

let mut local = chrono::Local
.datetime_from_str(&ts, format)
let local = NaiveDateTime::parse_from_str(&ts, format)
.map_err(|_| USimpleError::new(1, format!("invalid date ts format {}", ts.quote())))?;
let mut local = match chrono::Local.from_local_datetime(&local) {
LocalResult::Single(dt) => dt,
_ => {
return Err(USimpleError::new(

Check warning on line 422 in src/uu/touch/src/touch.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/touch/src/touch.rs#L422

Added line #L422 was not covered by tests
1,
format!("invalid date ts format {}", ts.quote()),

Check warning on line 424 in src/uu/touch/src/touch.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/touch/src/touch.rs#L424

Added line #L424 was not covered by tests
))
}
};

// Chrono caps seconds at 59, but 60 is valid. It might be a leap second
// or wrap to the next minute. But that doesn't really matter, because we
Expand Down
3 changes: 1 addition & 2 deletions tests/by-util/test_touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// spell-checker:ignore (formats) cymdhm cymdhms mdhm mdhms ymdhm ymdhms datetime mktime

use crate::common::util::{AtPath, TestScenario};
use chrono::TimeZone;
use filetime::{self, FileTime};
use std::fs::remove_file;
use std::path::PathBuf;
Expand All @@ -32,7 +31,7 @@ fn set_file_times(at: &AtPath, path: &str, atime: FileTime, mtime: FileTime) {
}

fn str_to_filetime(format: &str, s: &str) -> FileTime {
let tm = chrono::Utc.datetime_from_str(s, format).unwrap();
let tm = chrono::NaiveDateTime::parse_from_str(s, format).unwrap();
FileTime::from_unix_time(tm.timestamp(), tm.timestamp_subsec_nanos())
}

Expand Down

0 comments on commit 6ce8075

Please sign in to comment.