summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Hurst <tom@hur.st>2020-05-19 02:31:15 +0000
committerThomas Hurst <tom@hur.st>2020-05-19 02:31:15 +0000
commitacb7c49abf955691469473d92fe9cb2fbfde3edc (patch)
tree217596741245350ea6d7d8566e7eb9e10617fe86
parente54e1f53c83be3e62dc945d44cb569b79ac68ec7 (diff)
Improve handling of unavailable timestamps.
Previously if a timestamp was unavailable, it defaulted to the epoch. Prior to this it defaulted to a zero duration. Switch to an Option<SystemTime> and move the handling of unavailable timestamps to rendering.
-rw-r--r--src/fs/file.rs45
-rw-r--r--src/output/render/times.rs21
-rw-r--r--src/output/time.rs7
3 files changed, 34 insertions, 39 deletions
diff --git a/src/fs/file.rs b/src/fs/file.rs
index 35b2940..5745208 100644
--- a/src/fs/file.rs
+++ b/src/fs/file.rs
@@ -325,37 +325,36 @@ impl<'dir> File<'dir> {
}
}
- /// This file’s last modified timestamp.
- /// If the file's time is invalid, assume it was modified at the epoch
- pub fn modified_time(&self) -> SystemTime {
- self.metadata.modified().unwrap_or(UNIX_EPOCH)
+ /// This file’s last modified timestamp, if available on this platform.
+ pub fn modified_time(&self) -> Option<SystemTime> {
+ self.metadata.modified().ok()
}
- /// This file’s last changed timestamp.
- pub fn changed_time(&self) -> SystemTime {
+ /// This file’s last changed timestamp, if available on this platform.
+ pub fn changed_time(&self) -> Option<SystemTime> {
let (mut sec, mut nsec) = (self.metadata.ctime(), self.metadata.ctime_nsec());
- if sec < 0 { // yeah right
- if nsec > 0 {
- sec += 1;
- nsec = nsec - 1_000_000_000;
- }
- UNIX_EPOCH - Duration::new(sec.abs() as u64, nsec.abs() as u32)
- } else {
- UNIX_EPOCH + Duration::new(sec as u64, nsec as u32)
- }
+ Some(
+ if sec < 0 {
+ if nsec > 0 {
+ sec += 1;
+ nsec = nsec - 1_000_000_000;
+ }
+ UNIX_EPOCH - Duration::new(sec.abs() as u64, nsec.abs() as u32)
+ } else {
+ UNIX_EPOCH + Duration::new(sec as u64, nsec as u32)
+ }
+ )
}
- /// This file’s last accessed timestamp.
- /// If the file's time is invalid, assume it was accessed at the epoch
- pub fn accessed_time(&self) -> SystemTime {
- self.metadata.accessed().unwrap_or(UNIX_EPOCH)
+ /// This file’s last accessed timestamp, if available on this platform.
+ pub fn accessed_time(&self) -> Option<SystemTime> {
+ self.metadata.accessed().ok()
}
- /// This file’s created timestamp.
- /// If the file's time is invalid, assume it was created at the epoch
- pub fn created_time(&self) -> SystemTime {
- self.metadata.created().unwrap_or(UNIX_EPOCH)
+ /// This file’s created timestamp, if available on this platform.
+ pub fn created_time(&self) -> Option<SystemTime> {
+ self.metadata.created().ok()
}
/// This file’s ‘type’.
diff --git a/src/output/render/times.rs b/src/output/render/times.rs
index 2b3d000..135be35 100644
--- a/src/output/render/times.rs
+++ b/src/output/render/times.rs
@@ -11,18 +11,21 @@ pub trait Render {
format: &TimeFormat) -> TextCell;
}
-impl Render for std::time::SystemTime {
+impl Render for Option<std::time::SystemTime> {
fn render(self, style: Style,
tz: &Option<TimeZone>,
format: &TimeFormat) -> TextCell {
- if let Some(ref tz) = *tz {
- let datestamp = format.format_zoned(self, tz);
- TextCell::paint(style, datestamp)
- }
- else {
- let datestamp = format.format_local(self);
- TextCell::paint(style, datestamp)
- }
+ let datestamp = if let Some(time) = self {
+ if let Some(ref tz) = tz {
+ format.format_zoned(time, tz)
+ } else {
+ format.format_local(time)
+ }
+ } else {
+ String::from("-")
+ };
+
+ TextCell::paint(style, datestamp)
}
}
diff --git a/src/output/time.rs b/src/output/time.rs
index 13a6a1d..b168c6b 100644
--- a/src/output/time.rs
+++ b/src/output/time.rs
@@ -147,9 +147,6 @@ impl DefaultFormat {
#[allow(trivial_numeric_casts)]
fn format_local(&self, time: SystemTime) -> String {
- if time == UNIX_EPOCH {
- return "-".to_string();
- }
let date = LocalDateTime::at(systemtime_epoch(time));
if self.is_recent(date) {
@@ -164,10 +161,6 @@ impl DefaultFormat {
#[allow(trivial_numeric_casts)]
fn format_zoned(&self, time: SystemTime, zone: &TimeZone) -> String {
- if time == UNIX_EPOCH {
- return "-".to_string();
- }
-
let date = zone.to_zoned(LocalDateTime::at(systemtime_epoch(time)));
if self.is_recent(date) {