Просмотр исходного кода

tau: prevent panic in due_as_timestamp() plus minor fix in filter

Dastan-glitch 4 лет назад
Родитель
Сommit
d0319b6b73
2 измененных файлов с 5 добавлено и 6 удалено
  1. 3 4
      bin/tau/tau-cli/src/filter.rs
  2. 2 2
      bin/tau/tau-cli/src/util.rs

+ 3 - 4
bin/tau/tau-cli/src/filter.rs

@@ -1,4 +1,4 @@
-use chrono::{Datelike, NaiveDate, NaiveDateTime};
+use chrono::{Datelike, NaiveDateTime, Utc};
 use serde_json::Value;
 
 use crate::{primitives::TaskInfo, TaskEvent};
@@ -18,12 +18,11 @@ pub fn apply_filter(tasks: &mut Vec<TaskInfo>, filter: &str) {
             let (month, year) =
                 (filter[..2].parse::<u32>().unwrap(), filter[2..].parse::<i32>().unwrap());
 
-            let year = year + 2000;
+            let year = year + (Utc::today().year() / 100) * 100;
             tasks.retain(|task| {
                 let date = task.created_at;
                 let task_date = NaiveDateTime::from_timestamp(date, 0).date();
-                let filter_date = NaiveDate::from_ymd(year, month, 1);
-                task_date.month() == filter_date.month() && task_date.year() == filter_date.year()
+                task_date.month() == month && task_date.year() == year
             })
         }
 

+ 2 - 2
bin/tau/tau-cli/src/util.rs

@@ -12,8 +12,8 @@ use darkfi::{util::Timestamp, Result};
 
 /// Parse due date (e.g. "1503" for 15 March) as i64 timestamp.
 pub fn due_as_timestamp(due: &str) -> Option<i64> {
-    if due.len() != 4 {
-        error!("Due date must be of length 4 (e.g. \"1503\" for 15 March)");
+    if due.len() != 4 || !due.parse::<u32>().is_ok() {
+        error!("Due date must be digits of length 4 (e.g. \"1503\" for 15 March)");
         return None
     }
     let (day, month) = (due[..2].parse::<u32>().unwrap(), due[2..].parse::<u32>().unwrap());