diff --git a/src/filetransfer/ftp_transfer.rs b/src/filetransfer/ftp_transfer.rs index 6ac4f74..452749e 100644 --- a/src/filetransfer/ftp_transfer.rs +++ b/src/filetransfer/ftp_transfer.rs @@ -24,6 +24,7 @@ */ // Dependencies +extern crate chrono; extern crate ftp; extern crate regex; @@ -239,6 +240,8 @@ impl FileTransfer for FtpFileTransfer { (owner_pex, group_pex, others_pex) }; // Parse mtime and convert to SystemTime + // NOTE: two possible time syntax: if %Y is this.year => %b %d %H:%M, otherwise %b %d %Y + // Return } } diff --git a/src/utils.rs b/src/utils.rs index 40b8fc1..7f3a1fe 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -29,8 +29,9 @@ extern crate whoami; use crate::filetransfer::FileTransferProtocol; +use chrono::format::ParseError; use chrono::prelude::*; -use std::time::SystemTime; +use std::time::{Duration, SystemTime}; /// ### parse_remote_opt /// @@ -140,6 +141,33 @@ pub fn time_to_str(time: SystemTime, fmt: &str) -> String { format!("{}", datetime.format(fmt)) } +/// ### lstime_to_systime +/// +/// Convert ls syntax time to System Time +/// ls time has two possible syntax: +/// 1. if year is current: %b %d %H:%M (e.g. Nov 5 13:46) +/// 2. else: %b %d %Y (e.g. Nov 5 2019) +pub fn lstime_to_systime(tm: &str) -> Result { + let datetime: NaiveDateTime = match NaiveDate::parse_from_str(tm, "%b %d %Y") { + Ok(date) => { // Case 2. + // Return NaiveDateTime from NaiveDate with time 00:00:00 + date.and_hms(0, 0, 0) + } + Err(_) => { // Might be case 1. + // Check if syntax is case 1 + match NaiveDateTime::parse_from_str(tm, "%b %d %H:%M").err().unwrap() { // This will never succeed + ParseError::NotEnough => { // Format is correct + // We need to add Current Year at the end of the string + } + _ => return Err() + } + } + }; + // Convert datetime to system time + let mut sys_time: SystemTime = SystemTime::UNIX_EPOCH; + Ok(sys_time.checked_add(Duration::from_secs(datetime.timestamp() as u64)).unwrap_or(SystemTime::UNIX_EPOCH) +} + #[cfg(test)] mod tests {