Add full-iso time style

This commit is contained in:
Benjamin Sago 2017-07-06 00:21:38 +01:00
parent 786e8f4d7f
commit f0eed9fde4
4 changed files with 50 additions and 22 deletions

View File

@ -240,13 +240,14 @@ impl TimeFormat {
/// Determine how time should be formatted in timestamp columns.
fn deduce(matches: &getopts::Matches) -> Result<TimeFormat, Misfire> {
pub use output::time::{DefaultFormat, LongISO};
const STYLES: &[&str] = &["default", "long-iso"];
pub use output::time::{DefaultFormat};
const STYLES: &[&str] = &["default", "long-iso", "full-iso"];
if let Some(word) = matches.opt_str("time-style") {
match &*word {
"default" => Ok(TimeFormat::DefaultFormat(DefaultFormat::new())),
"long-iso" => Ok(TimeFormat::LongISO(LongISO)),
"long-iso" => Ok(TimeFormat::LongISO),
"full-iso" => Ok(TimeFormat::FullISO),
otherwise => Err(Misfire::bad_argument("time-style", otherwise, STYLES))
}
}

View File

@ -7,21 +7,24 @@ use fs::fields::Time;
pub enum TimeFormat {
DefaultFormat(DefaultFormat),
LongISO(LongISO),
LongISO,
FullISO,
}
impl TimeFormat {
pub fn format_local(&self, time: Time) -> String {
match *self {
TimeFormat::DefaultFormat(ref fmt) => fmt.format_local(time),
TimeFormat::LongISO(ref iso) => iso.format_local(time),
TimeFormat::LongISO => long_local(time),
TimeFormat::FullISO => full_local(time),
}
}
pub fn format_zoned(&self, time: Time, zone: &TimeZone) -> String {
match *self {
TimeFormat::DefaultFormat(ref fmt) => fmt.format_zoned(time, zone),
TimeFormat::LongISO(ref iso) => iso.format_zoned(time, zone),
TimeFormat::LongISO => long_zoned(time, zone),
TimeFormat::FullISO => full_zoned(time, zone),
}
}
}
@ -101,20 +104,40 @@ impl DefaultFormat {
}
pub struct LongISO;
impl LongISO {
#[allow(trivial_numeric_casts)]
fn format_local(&self, time: Time) -> String {
let date = LocalDateTime::at(time.seconds 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 format_zoned(&self, time: Time, zone: &TimeZone) -> String {
let date = zone.to_zoned(LocalDateTime::at(time.seconds 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_local(time: Time) -> String {
let date = LocalDateTime::at(time.seconds 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));
format!("{:04}-{:02}-{:02} {:02}:{:02}",
date.year(), date.month() as usize, date.day(),
date.hour(), date.minute())
}
#[allow(trivial_numeric_casts)]
fn full_local(time: Time) -> String {
let date = LocalDateTime::at(time.seconds 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)
}
#[allow(trivial_numeric_casts)]
fn full_zoned(time: Time, zone: &TimeZone) -> String {
use datetime::Offset;
let local = LocalDateTime::at(time.seconds 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,
offset.hours(), offset.minutes().abs())
}

3
xtests/dates_full_iso Normal file
View File

@ -0,0 +1,3 @@
.rw-rw-r-- 0 cassowary 2006-06-15 23:14:29.000000000 +0000 peach
.rw-rw-r-- 0 cassowary 2003-03-03 00:00:00.000000000 +0000 pear
.rw-rw-r-- 0 cassowary 2009-07-22 10:38:53.000000000 +0000 plum

View File

@ -111,6 +111,7 @@ $exa $testcases/file-names-exts/music.* -I "*.OGG|*.mp3" -1 2>&1 | diff -q - $re
$exa $testcases/dates -lh --accessed --sort=accessed 2>&1 | diff -q - $results/dates_accessed || exit 1
$exa $testcases/dates -lh --sort=modified 2>&1 | diff -q - $results/dates_modified || exit 1
$exa $testcases/dates -l --time-style=long-iso 2>&1 | diff -q - $results/dates_long_iso || exit 1
$exa $testcases/dates -l --time-style=full-iso 2>&1 | diff -q - $results/dates_full_iso || exit 1
# Paths and directories