Working on ls time parser

This commit is contained in:
ChristianVisintin
2020-12-02 17:00:52 +01:00
parent 5d4b255e26
commit 8dc5995458
2 changed files with 32 additions and 1 deletions

View File

@@ -24,6 +24,7 @@
*/ */
// Dependencies // Dependencies
extern crate chrono;
extern crate ftp; extern crate ftp;
extern crate regex; extern crate regex;
@@ -239,6 +240,8 @@ impl FileTransfer for FtpFileTransfer {
(owner_pex, group_pex, others_pex) (owner_pex, group_pex, others_pex)
}; };
// Parse mtime and convert to SystemTime // 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 // Return
} }
} }

View File

@@ -29,8 +29,9 @@ extern crate whoami;
use crate::filetransfer::FileTransferProtocol; use crate::filetransfer::FileTransferProtocol;
use chrono::format::ParseError;
use chrono::prelude::*; use chrono::prelude::*;
use std::time::SystemTime; use std::time::{Duration, SystemTime};
/// ### parse_remote_opt /// ### parse_remote_opt
/// ///
@@ -140,6 +141,33 @@ pub fn time_to_str(time: SystemTime, fmt: &str) -> String {
format!("{}", datetime.format(fmt)) 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<SystemTime, ParseError> {
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)] #[cfg(test)]
mod tests { mod tests {