Expect different time formats

This commit is contained in:
Benjamin Sago 2017-07-05 23:27:48 +01:00
parent aa5b1867dd
commit 98b63705be
2 changed files with 65 additions and 34 deletions

View File

@ -6,7 +6,7 @@ use output::Colours;
use output::{grid, details};
use output::table::{TimeTypes, Environment, SizeFormat, Options as TableOptions};
use output::file_name::Classify;
use output::time::TimeFormat;
use output::time::{TimeFormat, DefaultFormat};
use options::Misfire;
use fs::feature::xattr;
@ -199,7 +199,7 @@ impl TableOptions {
fn deduce(matches: &getopts::Matches) -> Result<Self, Misfire> {
Ok(TableOptions {
env: Environment::load_all(),
time_format: TimeFormat::deduce(),
time_format: TimeFormat::deduce(matches)?,
size_format: SizeFormat::deduce(matches)?,
time_types: TimeTypes::deduce(matches)?,
inode: matches.opt_present("inode"),
@ -236,6 +236,15 @@ impl SizeFormat {
}
impl TimeFormat {
/// Determine how time should be formatted in timestamp columns.
fn deduce(_matches: &getopts::Matches) -> Result<TimeFormat, Misfire> {
Ok(TimeFormat::DefaultFormat(DefaultFormat::new()))
}
}
impl TimeTypes {
/// Determine which of a files time fields should be displayed for it

View File

@ -5,8 +5,27 @@ use locale;
use fs::fields::Time;
pub enum TimeFormat {
DefaultFormat(DefaultFormat),
}
impl TimeFormat {
pub fn format_local(&self, time: Time) -> String {
match *self {
TimeFormat::DefaultFormat(ref fmt) => fmt.format_local(time),
}
}
pub fn format_zoned(&self, time: Time, zone: &TimeZone) -> String {
match *self {
TimeFormat::DefaultFormat(ref fmt) => fmt.format_zoned(time, zone),
}
}
}
#[derive(Debug, Clone)]
pub struct TimeFormat {
pub struct DefaultFormat {
/// The year of the current time. This gets used to determine which date
/// format to use.
@ -22,36 +41,8 @@ pub struct TimeFormat {
pub date_and_year: DateFormat<'static>,
}
impl TimeFormat {
fn is_recent(&self, date: LocalDateTime) -> bool {
date.year() == self.current_year
}
#[allow(trivial_numeric_casts)]
pub fn format_local(&self, time: Time) -> String {
let date = LocalDateTime::at(time.seconds as i64);
if self.is_recent(date) {
self.date_and_time.format(&date, &self.locale)
}
else {
self.date_and_year.format(&date, &self.locale)
}
}
#[allow(trivial_numeric_casts)]
pub fn format_zoned(&self, time: Time, zone: &TimeZone) -> String {
let date = zone.to_zoned(LocalDateTime::at(time.seconds as i64));
if self.is_recent(date) {
self.date_and_time.format(&date, &self.locale)
}
else {
self.date_and_year.format(&date, &self.locale)
}
}
pub fn deduce() -> TimeFormat {
impl DefaultFormat {
pub fn new() -> DefaultFormat {
use unicode_width::UnicodeWidthStr;
let locale = locale::Time::load_user_locale()
@ -74,6 +65,37 @@ impl TimeFormat {
_ => DateFormat::parse("{2>:D} {:M} {5>:Y}").unwrap()
};
TimeFormat { current_year, locale, date_and_time, date_and_year }
DefaultFormat { current_year, locale, date_and_time, date_and_year }
}
fn is_recent(&self, date: LocalDateTime) -> bool {
date.year() == self.current_year
}
}
impl DefaultFormat {
#[allow(trivial_numeric_casts)]
fn format_local(&self, time: Time) -> String {
let date = LocalDateTime::at(time.seconds as i64);
if self.is_recent(date) {
self.date_and_time.format(&date, &self.locale)
}
else {
self.date_and_year.format(&date, &self.locale)
}
}
#[allow(trivial_numeric_casts)]
fn format_zoned(&self, time: Time, zone: &TimeZone) -> String {
let date = zone.to_zoned(LocalDateTime::at(time.seconds as i64));
if self.is_recent(date) {
self.date_and_time.format(&date, &self.locale)
}
else {
self.date_and_year.format(&date, &self.locale)
}
}
}