|
| 1 | +// This file is part of the uutils coreutils package. |
| 2 | +// |
| 3 | +// For the full copyright and license information, please view the LICENSE |
| 4 | +// file that was distributed with this source code. |
| 5 | +use crate::error::{FromIo, UError, UResult, USimpleError}; |
| 6 | +use crate::parse_date_common::{ |
| 7 | + dt_to_filename, local_dt_to_filetime, to_local, ISO_8601_FORMAT, POSIX_LOCALE_FORMAT, |
| 8 | + YYMMDDHHMM_DOT_SS_FORMAT, YYMMDDHHMM_FORMAT, YYYYMMDDHHMMSS_FORMAT, YYYYMMDDHHMMS_FORMAT, |
| 9 | + YYYYMMDDHHMM_DOT_SS_FORMAT, YYYYMMDDHHMM_FORMAT, YYYYMMDDHHMM_OFFSET_FORMAT, |
| 10 | + YYYY_MM_DD_HH_MM_FORMAT, |
| 11 | +}; |
| 12 | +use crate::parse_relative_time; |
| 13 | +use filetime::{set_symlink_file_times, FileTime}; |
| 14 | +use time::macros::{format_description, offset, time}; |
| 15 | +use time::{format_description, OffsetDateTime, PrimitiveDateTime}; |
| 16 | + |
| 17 | +pub fn from_str(s: &str) -> UResult<FileTime> { |
| 18 | + // This isn't actually compatible with GNU touch, but there doesn't seem to |
| 19 | + // be any simple specification for what format this parameter allows and I'm |
| 20 | + // not about to implement GNU parse_datetime. |
| 21 | + // http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob_plain;f=lib/parse-datetime.y |
| 22 | + |
| 23 | + // TODO: match on char count? |
| 24 | + |
| 25 | + // "The preferred date and time representation for the current locale." |
| 26 | + // "(In the POSIX locale this is equivalent to %a %b %e %H:%M:%S %Y.)" |
| 27 | + // time 0.1.43 parsed this as 'a b e T Y' |
| 28 | + // which is equivalent to the POSIX locale: %a %b %e %H:%M:%S %Y |
| 29 | + // Tue Dec 3 ... |
| 30 | + // ("%c", POSIX_LOCALE_FORMAT), |
| 31 | + // |
| 32 | + if let Ok(parsed) = time::OffsetDateTime::parse(s, &POSIX_LOCALE_FORMAT) { |
| 33 | + return Ok(local_dt_to_filetime(to_local(parsed))); |
| 34 | + } |
| 35 | + |
| 36 | + // Also support other formats found in the GNU tests like |
| 37 | + // in tests/misc/stat-nanoseconds.sh |
| 38 | + // or tests/touch/no-rights.sh |
| 39 | + for fmt in [ |
| 40 | + YYYYMMDDHHMMS_FORMAT, |
| 41 | + YYYYMMDDHHMMSS_FORMAT, |
| 42 | + YYYY_MM_DD_HH_MM_FORMAT, |
| 43 | + YYYYMMDDHHMM_OFFSET_FORMAT, |
| 44 | + ] { |
| 45 | + if let Ok(parsed) = time::PrimitiveDateTime::parse(s, &fmt) { |
| 46 | + return Ok(dt_to_filename(parsed)); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // "Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)" |
| 51 | + // ("%F", ISO_8601_FORMAT), |
| 52 | + if let Ok(parsed) = time::Date::parse(s, &ISO_8601_FORMAT) { |
| 53 | + return Ok(local_dt_to_filetime(to_local( |
| 54 | + time::PrimitiveDateTime::new(parsed, time!(00:00)), |
| 55 | + ))); |
| 56 | + } |
| 57 | + |
| 58 | + // "@%s" is "The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). (TZ) (Calculated from mktime(tm).)" |
| 59 | + if s.bytes().next() == Some(b'@') { |
| 60 | + if let Ok(ts) = &s[1..].parse::<i64>() { |
| 61 | + // Don't convert to local time in this case - seconds since epoch are not time-zone dependent |
| 62 | + return Ok(local_dt_to_filetime( |
| 63 | + time::OffsetDateTime::from_unix_timestamp(*ts).unwrap(), |
| 64 | + )); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if let Some(duration) = parse_relative_time::from_str(s) { |
| 69 | + let now_local = time::OffsetDateTime::now_local().unwrap(); |
| 70 | + let diff = now_local.checked_add(duration).unwrap(); |
| 71 | + return Ok(local_dt_to_filetime(diff)); |
| 72 | + } |
| 73 | + |
| 74 | + Err(USimpleError::new(1, format!("Unable to parse date: {s}"))) |
| 75 | +} |
| 76 | + |
| 77 | +#[cfg(test)] |
| 78 | +mod tests { |
| 79 | + use super::*; |
| 80 | + use time::OffsetDateTime; |
| 81 | + |
| 82 | + fn assert_file_time_eq(s: &str, expected: FileTime) { |
| 83 | + let parsed = from_str(s).unwrap(); |
| 84 | + assert_eq!(parsed, expected); |
| 85 | + } |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn test_from_str_posix_locale_format() { |
| 89 | + let s = "2023-04-23T12:34:56"; |
| 90 | + let expected = local_dt_to_filetime(OffsetDateTime::from_str(s).unwrap()); |
| 91 | + assert_file_time_eq(s, expected); |
| 92 | + } |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn test_from_str_various_formats() { |
| 96 | + let formats = [ |
| 97 | + ("20230423123456", YYYYMMDDHHMMS_FORMAT), |
| 98 | + ("20230423123456.123", YYYYMMDDHHMMSS_FORMAT), |
| 99 | + ("2023-04-23_12-34", YYYY_MM_DD_HH_MM_FORMAT), |
| 100 | + ("202304231234-0500", YYYYMMDDHHMM_OFFSET_FORMAT), |
| 101 | + ]; |
| 102 | + |
| 103 | + for (s, fmt) in formats.iter() { |
| 104 | + let expected = dt_to_filename(time::PrimitiveDateTime::parse(s, fmt).unwrap()); |
| 105 | + assert_file_time_eq(s, expected); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + #[test] |
| 110 | + fn test_from_str_iso_8601_format() { |
| 111 | + let s = "2023-04-23"; |
| 112 | + let expected = local_dt_to_filetime( |
| 113 | + to_local(time::PrimitiveDateTime::new( |
| 114 | + time::Date::parse(s, &ISO_8601_FORMAT).unwrap(), |
| 115 | + time!(00:00), |
| 116 | + )), |
| 117 | + ); |
| 118 | + assert_file_time_eq(s, expected); |
| 119 | + } |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn test_from_str_seconds_since_epoch() { |
| 123 | + let s = "@1609459200"; |
| 124 | + let expected = local_dt_to_filetime( |
| 125 | + time::OffsetDateTime::from_unix_timestamp(1609459200).unwrap(), |
| 126 | + ); |
| 127 | + assert_file_time_eq(s, expected); |
| 128 | + } |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn test_from_str_relative_time() { |
| 132 | + let s = "+1d"; |
| 133 | + let duration = parse_relative_time::from_str(s).unwrap(); |
| 134 | + let now_local = time::OffsetDateTime::now_local().unwrap(); |
| 135 | + let expected = local_dt_to_filetime(now_local.checked_add(duration).unwrap()); |
| 136 | + let parsed = from_str(s).unwrap(); |
| 137 | + assert!(parsed.duration_since(expected).unwrap().abs() < time::Duration::seconds(1)); |
| 138 | + } |
| 139 | + |
| 140 | + #[test] |
| 141 | + fn test_from_str_invalid_input() { |
| 142 | + let s = "invalid"; |
| 143 | + assert!(from_str(s).is_err()); |
| 144 | + } |
| 145 | +} |
0 commit comments