Updated file.rs

Added checks to `file.rs` to ensure that file's metadata exists after `UNIX_EPOCH`.
If the file was accessed/modified/created after UNIX_EPOCH, the current day is displayed.
This commit is contained in:
Brennan McDonald 2019-07-31 15:43:31 -07:00 committed by GitHub
parent 89c861f24d
commit f19f7be726
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,7 +5,7 @@ use std::io::Error as IOError;
use std::io::Result as IOResult;
use std::os::unix::fs::{MetadataExt, PermissionsExt, FileTypeExt};
use std::path::{Path, PathBuf};
use std::time::{UNIX_EPOCH, Duration};
use std::time::{SystemTime, UNIX_EPOCH, Duration};
use fs::dir::Dir;
use fs::fields as f;
@ -327,8 +327,13 @@ impl<'dir> File<'dir> {
}
/// This files last modified timestamp.
/// If the file's time is invalid, assume it was modified today
pub fn modified_time(&self) -> Duration {
self.metadata.modified().unwrap().duration_since(UNIX_EPOCH).unwrap()
if self.metadata.modified().unwrap() < UNIX_EPOCH {
return SystemTime::now().duration_since(UNIX_EPOCH).unwrap()
} else {
return self.metadata.created().unwrap().duration_since(UNIX_EPOCH).unwrap()
}
}
/// This files last changed timestamp.
@ -337,13 +342,23 @@ impl<'dir> File<'dir> {
}
/// This files last accessed timestamp.
/// If the file's time is invalid, assume it was accessed today
pub fn accessed_time(&self) -> Duration {
self.metadata.accessed().unwrap().duration_since(UNIX_EPOCH).unwrap()
if self.metadata.modified().unwrap() < UNIX_EPOCH{
return SystemTime::now().duration_since(UNIX_EPOCH).unwrap()
} else {
return self.metadata.created().unwrap().duration_since(UNIX_EPOCH).unwrap()
}
}
/// This files created timestamp.
/// If the file's time is invalid, assume it was created today
pub fn created_time(&self) -> Duration {
self.metadata.created().unwrap().duration_since(UNIX_EPOCH).unwrap()
if self.metadata.modified().unwrap() < UNIX_EPOCH {
return SystemTime::now().duration_since(UNIX_EPOCH).unwrap()
} else {
return self.metadata.created().unwrap().duration_since(UNIX_EPOCH).unwrap()
}
}
/// This files type.