mirror of
https://github.com/Llewellynvdm/exa.git
synced 2024-11-26 13:56:27 +00:00
Add iso time style
This commit is contained in:
parent
f0eed9fde4
commit
3251378e91
@ -240,15 +240,16 @@ impl TimeFormat {
|
|||||||
|
|
||||||
/// Determine how time should be formatted in timestamp columns.
|
/// Determine how time should be formatted in timestamp columns.
|
||||||
fn deduce(matches: &getopts::Matches) -> Result<TimeFormat, Misfire> {
|
fn deduce(matches: &getopts::Matches) -> Result<TimeFormat, Misfire> {
|
||||||
pub use output::time::{DefaultFormat};
|
pub use output::time::{DefaultFormat, ISOFormat};
|
||||||
const STYLES: &[&str] = &["default", "long-iso", "full-iso"];
|
const STYLES: &[&str] = &["default", "long-iso", "full-iso", "iso"];
|
||||||
|
|
||||||
if let Some(word) = matches.opt_str("time-style") {
|
if let Some(word) = matches.opt_str("time-style") {
|
||||||
match &*word {
|
match &*word {
|
||||||
"default" => Ok(TimeFormat::DefaultFormat(DefaultFormat::new())),
|
"default" => Ok(TimeFormat::DefaultFormat(DefaultFormat::new())),
|
||||||
|
"iso" => Ok(TimeFormat::ISOFormat(ISOFormat::new())),
|
||||||
"long-iso" => Ok(TimeFormat::LongISO),
|
"long-iso" => Ok(TimeFormat::LongISO),
|
||||||
"full-iso" => Ok(TimeFormat::FullISO),
|
"full-iso" => Ok(TimeFormat::FullISO),
|
||||||
otherwise => Err(Misfire::bad_argument("time-style", otherwise, STYLES))
|
otherwise => Err(Misfire::bad_argument("time-style", otherwise, STYLES)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -7,6 +7,7 @@ use fs::fields::Time;
|
|||||||
|
|
||||||
pub enum TimeFormat {
|
pub enum TimeFormat {
|
||||||
DefaultFormat(DefaultFormat),
|
DefaultFormat(DefaultFormat),
|
||||||
|
ISOFormat(ISOFormat),
|
||||||
LongISO,
|
LongISO,
|
||||||
FullISO,
|
FullISO,
|
||||||
}
|
}
|
||||||
@ -15,6 +16,7 @@ impl TimeFormat {
|
|||||||
pub fn format_local(&self, time: Time) -> String {
|
pub fn format_local(&self, time: Time) -> String {
|
||||||
match *self {
|
match *self {
|
||||||
TimeFormat::DefaultFormat(ref fmt) => fmt.format_local(time),
|
TimeFormat::DefaultFormat(ref fmt) => fmt.format_local(time),
|
||||||
|
TimeFormat::ISOFormat(ref iso) => iso.format_local(time),
|
||||||
TimeFormat::LongISO => long_local(time),
|
TimeFormat::LongISO => long_local(time),
|
||||||
TimeFormat::FullISO => full_local(time),
|
TimeFormat::FullISO => full_local(time),
|
||||||
}
|
}
|
||||||
@ -23,6 +25,7 @@ impl TimeFormat {
|
|||||||
pub fn format_zoned(&self, time: Time, zone: &TimeZone) -> String {
|
pub fn format_zoned(&self, time: Time, zone: &TimeZone) -> String {
|
||||||
match *self {
|
match *self {
|
||||||
TimeFormat::DefaultFormat(ref fmt) => fmt.format_zoned(time, zone),
|
TimeFormat::DefaultFormat(ref fmt) => fmt.format_zoned(time, zone),
|
||||||
|
TimeFormat::ISOFormat(ref iso) => iso.format_zoned(time, zone),
|
||||||
TimeFormat::LongISO => long_zoned(time, zone),
|
TimeFormat::LongISO => long_zoned(time, zone),
|
||||||
TimeFormat::FullISO => full_zoned(time, zone),
|
TimeFormat::FullISO => full_zoned(time, zone),
|
||||||
}
|
}
|
||||||
@ -141,3 +144,54 @@ fn full_zoned(time: Time, zone: &TimeZone) -> String {
|
|||||||
date.hour(), date.minute(), date.second(), time.nanoseconds,
|
date.hour(), date.minute(), date.second(), time.nanoseconds,
|
||||||
offset.hours(), offset.minutes().abs())
|
offset.hours(), offset.minutes().abs())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct ISOFormat {
|
||||||
|
|
||||||
|
/// The year of the current time. This gets used to determine which date
|
||||||
|
/// format to use.
|
||||||
|
pub current_year: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ISOFormat {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let current_year = LocalDateTime::now().year();
|
||||||
|
ISOFormat { current_year }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_recent(&self, date: LocalDateTime) -> bool {
|
||||||
|
date.year() == self.current_year
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(trivial_numeric_casts)]
|
||||||
|
fn format_local(&self, time: Time) -> String {
|
||||||
|
let date = LocalDateTime::at(time.seconds as i64);
|
||||||
|
|
||||||
|
if self.is_recent(date) {
|
||||||
|
format!("{:04}-{:02}-{:02}",
|
||||||
|
date.year(), date.month() as usize, date.day())
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
format!("{:02}-{:02} {:02}:{:02}",
|
||||||
|
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));
|
||||||
|
|
||||||
|
if self.is_recent(date) {
|
||||||
|
format!("{:04}-{:02}-{:02}",
|
||||||
|
date.year(), date.month() as usize, date.day())
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
format!("{:02}-{:02} {:02}:{:02}",
|
||||||
|
date.month() as usize, date.day(),
|
||||||
|
date.hour(), date.minute())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
3
xtests/dates_iso
Normal file
3
xtests/dates_iso
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
.[1;33mr[31mw[0m[38;5;244m-[33mr[31mw[38;5;244m-[33mr[38;5;244m--[0m [1;32m0[0m cassowary [34m06-15 23:14[0m peach
|
||||||
|
.[1;33mr[31mw[0m[38;5;244m-[33mr[31mw[38;5;244m-[33mr[38;5;244m--[0m [1;32m0[0m cassowary [34m03-03 00:00[0m pear
|
||||||
|
.[1;33mr[31mw[0m[38;5;244m-[33mr[31mw[38;5;244m-[33mr[38;5;244m--[0m [1;32m0[0m cassowary [34m07-22 10:38[0m plum
|
@ -112,6 +112,7 @@ $exa $testcases/dates -lh --accessed --sort=accessed 2>&1 | diff -q - $results/d
|
|||||||
$exa $testcases/dates -lh --sort=modified 2>&1 | diff -q - $results/dates_modified || 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=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
|
$exa $testcases/dates -l --time-style=full-iso 2>&1 | diff -q - $results/dates_full_iso || exit 1
|
||||||
|
$exa $testcases/dates -l --time-style=iso 2>&1 | diff -q - $results/dates_iso || exit 1
|
||||||
|
|
||||||
|
|
||||||
# Paths and directories
|
# Paths and directories
|
||||||
|
Loading…
Reference in New Issue
Block a user