Pārlūkot izejas kodu

util/time.rs: replaced date format string with enum

aggstam 4 gadi atpakaļ
vecāks
revīzija
0d8fb66e53
2 mainītis faili ar 25 papildinājumiem un 14 dzēšanām
  1. 8 4
      bin/tau/tau-cli/src/view.rs
  2. 17 10
      src/util/time.rs

+ 8 - 4
bin/tau/tau-cli/src/view.rs

@@ -4,7 +4,10 @@ use prettytable::{
     row, table, Cell, Row, Table,
 };
 
-use darkfi::{util::time::timestamp_to_date, Result};
+use darkfi::{
+    util::time::{timestamp_to_date, DateFormat},
+    Result,
+};
 
 use crate::{
     filter::apply_filter,
@@ -57,7 +60,8 @@ pub fn print_task_list(tasks: Vec<TaskInfo>, filters: Vec<String>) -> Result<()>
             Cell::new(&task.title).style_spec(gen_style),
             Cell::new(&task.project.join(", ")).style_spec(gen_style),
             Cell::new(&task.assign.join(", ")).style_spec(gen_style),
-            Cell::new(&timestamp_to_date(task.due.unwrap_or(0), "date")).style_spec(gen_style),
+            Cell::new(&timestamp_to_date(task.due.unwrap_or(0), DateFormat::Date))
+                .style_spec(gen_style),
             if task.rank == max_rank {
                 Cell::new(&rank).style_spec(max_style)
             } else if task.rank == min_rank {
@@ -74,8 +78,8 @@ pub fn print_task_list(tasks: Vec<TaskInfo>, filters: Vec<String>) -> Result<()>
 
 pub fn print_task_info(taskinfo: TaskInfo) -> Result<()> {
     let current_state = &taskinfo.events.last().unwrap_or(&TaskEvent::default()).action.clone();
-    let due = timestamp_to_date(taskinfo.due.unwrap_or(0), "date");
-    let created_at = timestamp_to_date(taskinfo.created_at, "datetime");
+    let due = timestamp_to_date(taskinfo.due.unwrap_or(0), DateFormat::Date);
+    let created_at = timestamp_to_date(taskinfo.created_at, DateFormat::DateTime);
 
     let mut table = table!(
         [Bd => "ref_id", &taskinfo.ref_id],

+ 17 - 10
src/util/time.rs

@@ -1,7 +1,7 @@
 use std::{
     mem,
     net::UdpSocket,
-    time::{Duration, SystemTime},
+    time::{Duration, UNIX_EPOCH},
 };
 
 use async_std::{
@@ -54,7 +54,7 @@ impl Timestamp {
 
 impl std::fmt::Display for Timestamp {
     fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
-        let date = timestamp_to_date(self.0, "datetime");
+        let date = timestamp_to_date(self.0, DateFormat::DateTime);
         write!(f, "{}", date)
     }
 }
@@ -79,7 +79,7 @@ impl NanoTimestamp {
 }
 impl std::fmt::Display for NanoTimestamp {
     fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
-        let date = timestamp_to_date(self.0, "nanos");
+        let date = timestamp_to_date(self.0, DateFormat::Nanos);
         write!(f, "{}", date)
     }
 }
@@ -190,28 +190,35 @@ async fn clock_check() -> Result<()> {
     }
 }
 
-pub fn timestamp_to_date(timestamp: i64, dt: &str) -> String {
+pub enum DateFormat {
+    Default,
+    Date,
+    DateTime,
+    Nanos,
+}
+
+pub fn timestamp_to_date(timestamp: i64, format: DateFormat) -> String {
     if timestamp <= 0 {
         return "".to_string()
     }
 
-    match dt {
-        "date" => {
+    match format {
+        DateFormat::Date => {
             NaiveDateTime::from_timestamp(timestamp, 0).date().format("%A %-d %B").to_string()
         }
-        "datetime" => {
+        DateFormat::DateTime => {
             NaiveDateTime::from_timestamp(timestamp, 0).format("%H:%M:%S %A %-d %B").to_string()
         }
-        "nanos" => {
+        DateFormat::Nanos => {
             const A_BILLION: i64 = 1_000_000_000;
             NaiveDateTime::from_timestamp(timestamp / A_BILLION, (timestamp % A_BILLION) as u32)
                 .format("%H:%M:%S.%f")
                 .to_string()
         }
-        _ => "".to_string(),
+        DateFormat::Default => "".to_string(),
     }
 }
 
 pub fn unix_timestamp() -> Result<u64> {
-    Ok(SystemTime::now().duration_since(std::time::UNIX_EPOCH)?.as_secs())
+    Ok(UNIX_EPOCH.elapsed()?.as_secs())
 }