From b0da0c90556e859b2ac1467985b47469d7108c76 Mon Sep 17 00:00:00 2001 From: ariasuni Date: Mon, 17 Dec 2018 02:51:55 +0100 Subject: [PATCH] Use correct metadata for created time --- src/fs/file.rs | 28 +++++++++--------------- src/output/render/mod.rs | 1 + src/output/render/times.rs | 11 +++++++--- src/output/table.rs | 1 + src/output/time.rs | 44 +++++++++++++++++++------------------- 5 files changed, 42 insertions(+), 43 deletions(-) diff --git a/src/fs/file.rs b/src/fs/file.rs index 4196419..e1ff61f 100644 --- a/src/fs/file.rs +++ b/src/fs/file.rs @@ -5,6 +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 fs::dir::Dir; use fs::fields as f; @@ -296,27 +297,18 @@ impl<'dir> File<'dir> { } /// This file’s last modified timestamp. - pub fn modified_time(&self) -> f::Time { - f::Time { - seconds: self.metadata.mtime(), - nanoseconds: self.metadata.mtime_nsec() - } - } - - /// This file’s created timestamp. - pub fn created_time(&self) -> f::Time { - f::Time { - seconds: self.metadata.ctime(), - nanoseconds: self.metadata.ctime_nsec() - } + pub fn modified_time(&self) -> Duration { + self.metadata.modified().unwrap().duration_since(UNIX_EPOCH).unwrap() } /// This file’s last accessed timestamp. - pub fn accessed_time(&self) -> f::Time { - f::Time { - seconds: self.metadata.atime(), - nanoseconds: self.metadata.atime_nsec() - } + pub fn accessed_time(&self) -> Duration { + self.metadata.accessed().unwrap().duration_since(UNIX_EPOCH).unwrap() + } + + /// This file’s created timestamp. + pub fn created_time(&self) -> Duration { + self.metadata.created().unwrap().duration_since(UNIX_EPOCH).unwrap() } /// This file’s ‘type’. diff --git a/src/output/render/mod.rs b/src/output/render/mod.rs index f641d77..f6248af 100644 --- a/src/output/render/mod.rs +++ b/src/output/render/mod.rs @@ -23,6 +23,7 @@ mod size; pub use self::size::Colours as SizeColours; mod times; +pub use self::times::Render as TimeRender; // times does too mod users; diff --git a/src/output/render/times.rs b/src/output/render/times.rs index 9f40b35..ce4a791 100644 --- a/src/output/render/times.rs +++ b/src/output/render/times.rs @@ -1,13 +1,18 @@ use datetime::TimeZone; use ansi_term::Style; -use fs::fields as f; use output::cell::TextCell; use output::time::TimeFormat; -impl f::Time { - pub fn render(self, style: Style, +pub trait Render { + fn render(self, style: Style, + tz: &Option, + format: &TimeFormat) -> TextCell; +} + +impl Render for std::time::Duration { + fn render(self, style: Style, tz: &Option, format: &TimeFormat) -> TextCell { diff --git a/src/output/table.rs b/src/output/table.rs index 2f3e6d7..1139fcd 100644 --- a/src/output/table.rs +++ b/src/output/table.rs @@ -12,6 +12,7 @@ use users::UsersCache; use style::Colours; use output::cell::TextCell; +use output::render::TimeRender; use output::time::TimeFormat; use fs::{File, fields as f}; use fs::feature::git::GitCache; diff --git a/src/output/time.rs b/src/output/time.rs index 6412767..cec1f34 100644 --- a/src/output/time.rs +++ b/src/output/time.rs @@ -1,12 +1,12 @@ //! Timestamp formatting. +use std::time::Duration; + use datetime::{LocalDateTime, TimeZone, DatePiece, TimePiece}; use datetime::fmt::DateFormat; use locale; use std::cmp; -use fs::fields::Time; - /// Every timestamp in exa needs to be rendered by a **time format**. /// Formatting times is tricky, because how a timestamp is rendered can @@ -51,7 +51,7 @@ pub enum TimeFormat { // timestamps are separate types. impl TimeFormat { - pub fn format_local(&self, time: Time) -> String { + pub fn format_local(&self, time: Duration) -> String { match *self { TimeFormat::DefaultFormat(ref fmt) => fmt.format_local(time), TimeFormat::ISOFormat(ref iso) => iso.format_local(time), @@ -60,7 +60,7 @@ impl TimeFormat { } } - pub fn format_zoned(&self, time: Time, zone: &TimeZone) -> String { + pub fn format_zoned(&self, time: Duration, zone: &TimeZone) -> String { match *self { TimeFormat::DefaultFormat(ref fmt) => fmt.format_zoned(time, zone), TimeFormat::ISOFormat(ref iso) => iso.format_zoned(time, zone), @@ -128,8 +128,8 @@ impl DefaultFormat { } #[allow(trivial_numeric_casts)] - fn format_local(&self, time: Time) -> String { - let date = LocalDateTime::at(time.seconds as i64); + fn format_local(&self, time: Duration) -> String { + let date = LocalDateTime::at(time.as_secs() as i64); if self.is_recent(date) { self.date_and_time.format(&date, &self.locale) @@ -140,8 +140,8 @@ impl DefaultFormat { } #[allow(trivial_numeric_casts)] - fn format_zoned(&self, time: Time, zone: &TimeZone) -> String { - let date = zone.to_zoned(LocalDateTime::at(time.seconds as i64)); + fn format_zoned(&self, time: Duration, zone: &TimeZone) -> String { + let date = zone.to_zoned(LocalDateTime::at(time.as_secs() as i64)); if self.is_recent(date) { self.date_and_time.format(&date, &self.locale) @@ -154,16 +154,16 @@ impl DefaultFormat { #[allow(trivial_numeric_casts)] -fn long_local(time: Time) -> String { - let date = LocalDateTime::at(time.seconds as i64); +fn long_local(time: Duration) -> String { + let date = LocalDateTime::at(time.as_secs() as i64); format!("{:04}-{:02}-{:02} {:02}:{:02}", date.year(), date.month() as usize, date.day(), date.hour(), date.minute()) } #[allow(trivial_numeric_casts)] -fn long_zoned(time: Time, zone: &TimeZone) -> String { - let date = zone.to_zoned(LocalDateTime::at(time.seconds as i64)); +fn long_zoned(time: Duration, zone: &TimeZone) -> String { + let date = zone.to_zoned(LocalDateTime::at(time.as_secs() as i64)); format!("{:04}-{:02}-{:02} {:02}:{:02}", date.year(), date.month() as usize, date.day(), date.hour(), date.minute()) @@ -171,23 +171,23 @@ fn long_zoned(time: Time, zone: &TimeZone) -> String { #[allow(trivial_numeric_casts)] -fn full_local(time: Time) -> String { - let date = LocalDateTime::at(time.seconds as i64); +fn full_local(time: Duration) -> String { + let date = LocalDateTime::at(time.as_secs() as i64); format!("{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:09}", date.year(), date.month() as usize, date.day(), - date.hour(), date.minute(), date.second(), time.nanoseconds) + date.hour(), date.minute(), date.second(), time.subsec_nanos()) } #[allow(trivial_numeric_casts)] -fn full_zoned(time: Time, zone: &TimeZone) -> String { +fn full_zoned(time: Duration, zone: &TimeZone) -> String { use datetime::Offset; - let local = LocalDateTime::at(time.seconds as i64); + let local = LocalDateTime::at(time.as_secs() as i64); let date = zone.to_zoned(local); let offset = Offset::of_seconds(zone.offset(local) as i32).expect("Offset out of range"); format!("{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:09} {:+03}{:02}", date.year(), date.month() as usize, date.day(), - date.hour(), date.minute(), date.second(), time.nanoseconds, + date.hour(), date.minute(), date.second(), time.subsec_nanos(), offset.hours(), offset.minutes().abs()) } @@ -214,8 +214,8 @@ impl ISOFormat { } #[allow(trivial_numeric_casts)] - fn format_local(&self, time: Time) -> String { - let date = LocalDateTime::at(time.seconds as i64); + fn format_local(&self, time: Duration) -> String { + let date = LocalDateTime::at(time.as_secs() as i64); if self.is_recent(date) { format!("{:02}-{:02} {:02}:{:02}", @@ -229,8 +229,8 @@ impl ISOFormat { } #[allow(trivial_numeric_casts)] - fn format_zoned(&self, time: Time, zone: &TimeZone) -> String { - let date = zone.to_zoned(LocalDateTime::at(time.seconds as i64)); + fn format_zoned(&self, time: Duration, zone: &TimeZone) -> String { + let date = zone.to_zoned(LocalDateTime::at(time.as_secs() as i64)); if self.is_recent(date) { format!("{:02}-{:02} {:02}:{:02}",