Benjamin Sago преди 8 години
родител
ревизия
3251378e91
променени са 4 файла, в които са добавени 62 реда и са изтрити 3 реда
  1. 4 3
      src/options/view.rs
  2. 54 0
      src/output/time.rs
  3. 3 0
      xtests/dates_iso
  4. 1 0
      xtests/run.sh

+ 4 - 3
src/options/view.rs

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

+ 54 - 0
src/output/time.rs

@@ -7,6 +7,7 @@ use fs::fields::Time;
 
 pub enum TimeFormat {
     DefaultFormat(DefaultFormat),
+    ISOFormat(ISOFormat),
     LongISO,
     FullISO,
 }
@@ -15,6 +16,7 @@ impl TimeFormat {
     pub fn format_local(&self, time: Time) -> String {
         match *self {
             TimeFormat::DefaultFormat(ref fmt) => fmt.format_local(time),
+            TimeFormat::ISOFormat(ref iso)     => iso.format_local(time),
             TimeFormat::LongISO                => long_local(time),
             TimeFormat::FullISO                => full_local(time),
         }
@@ -23,6 +25,7 @@ impl TimeFormat {
     pub fn format_zoned(&self, time: Time, zone: &TimeZone) -> String {
         match *self {
             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::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,
             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 - 0
xtests/dates_iso

@@ -0,0 +1,3 @@
+.rw-rw-r-- 0 cassowary 06-15 23:14 peach
+.rw-rw-r-- 0 cassowary 03-03 00:00 pear
+.rw-rw-r-- 0 cassowary 07-22 10:38 plum

+ 1 - 0
xtests/run.sh

@@ -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 -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=iso 2>&1 | diff -q - $results/dates_iso       || exit 1
 
 
 # Paths and directories