From b27c370a455a8002a0194c5a937882676c2e6e65 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Tue, 12 May 2020 19:49:56 -0300 Subject: [PATCH] Deal with times --- Cargo.toml | 6 ++--- README.md | 6 ++--- src/eventlist/event.rs | 56 ++++++++++++++++++++++++++++++++++++------ src/main.rs | 13 +++++++++- 4 files changed, 66 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a5b834d..a35ea30 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,11 +6,11 @@ authors = ["Julio Biason "] edition = "2018" [dependencies] -chrono = "0.4.11" -clap = "2.33.0" +chrono = "0.4" +clap = "2.33" serde = "*" serde_derive = "*" toml = "0.5" uuid = { version = "0.8", features = ["v4"] } log = "*" -env_logger = "0.7.1" +env_logger = "0.7" diff --git a/README.md b/README.md index 5817a89..9351be9 100644 --- a/README.md +++ b/README.md @@ -22,15 +22,15 @@ tell you that you have 15 days up to it. - [x] Add unique identifier for each event - [x] List events -- [ ] Add Events with time -- [ ] List events with time +- [x] Add Events with time +- [x] List events with time - [x] Sort events by ETA - [ ] Option to remove events +- [ ] Automatically remove past events - [ ] Move the app "db" to a fixed space - [x] Replace `dbg!` with [env_logger](https://crates.io/crates/env_logger) (reasoning: Although `dbg!` is nice and dandy, it can't be disabled, and that's bad UI) -- [ ] Remove `unwrap()`s ## License diff --git a/src/eventlist/event.rs b/src/eventlist/event.rs index 5f86e8b..254cfb1 100644 --- a/src/eventlist/event.rs +++ b/src/eventlist/event.rs @@ -46,11 +46,13 @@ pub enum EventDateType { AtTime(Date, Time), } -impl From<&EventDateType> for DateTime { +impl From<&EventDateType> for DateTime { fn from(origin: &EventDateType) -> Self { match origin { - EventDateType::AllDay(d) => Utc.ymd(d.year, d.month, d.day).and_hms(0, 0, 0), - _ => Utc.ymd(1970, 1, 1).and_hms(0, 0, 0), + EventDateType::AllDay(d) => Local.ymd(d.year, d.month, d.day).and_hms(0, 0, 0), + EventDateType::AtTime(d, t) => { + Local.ymd(d.year, d.month, d.day).and_hms(t.hour, t.min, 0) + } } } } @@ -73,13 +75,17 @@ pub struct Event { due: EventDateType, } +fn uuid() -> String { + let (id, _, _, _) = Uuid::new_v4().as_fields(); + format!("{:x}", id) +} + impl Event { pub fn new_on_date(description: &str, date: &str) -> Self { let fake_datetime = format!("{} 00:00:00", date); if let Ok(dt) = Utc.datetime_from_str(&fake_datetime, "%Y-%m-%d %H:%M:%S") { - let (id, _, _, _) = Uuid::new_v4().as_fields(); Self { - id: format!("{:x}", id), + id: uuid(), description: description.into(), due: EventDateType::AllDay(Date { year: dt.year(), @@ -92,11 +98,45 @@ impl Event { } } + pub fn new_on_date_time(description: &str, date: &str, time: &str) -> Self { + let fake_datetime = format!("{} {}:00", date, time); + if let Ok(dt) = Utc.datetime_from_str(&fake_datetime, "%Y-%m-%d %H:%M:%S") { + Self { + id: uuid(), + description: description.into(), + due: EventDateType::AtTime( + Date { + year: dt.year(), + month: dt.month(), + day: dt.day(), + }, + Time { + hour: dt.hour(), + min: dt.minute(), + }, + ), + } + } else { + panic!("Failed to parse the date"); + } + } + pub fn eta(&self) -> Option { - let now = Utc::now(); - let to: DateTime = (&self.due).into(); + let now = Local::now(); + let to: DateTime = (&self.due).into(); let eta = to - now; - Some(format!("{}d", eta.num_days())) + log::debug!("ETA for {}: {}", self.id, eta.num_minutes()); + + match self.due { + EventDateType::AllDay(_) if eta.num_minutes() > 0 => { + Some(format!("{}d", eta.num_days())) + } + EventDateType::AtTime(_, _) if eta.num_minutes() > 0 => match eta.num_days() { + 0 => Some(format!("{}h", eta.num_hours())), + _ => Some(format!("{}d {}h", eta.num_days(), eta.num_hours())), + }, + _ => None, + } } } diff --git a/src/main.rs b/src/main.rs index a7d391c..a663968 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,7 +32,9 @@ fn main() { match command { args::Action::List => list(), args::Action::Add(description, date) => add_with_date(&description, &date), - _ => println!("Command not implemented yet"), + args::Action::AddWithTime(description, date, time) => { + add_with_date_time(&description, &date, &time) + } } } } @@ -46,6 +48,15 @@ fn list() { fn add_with_date(description: &str, date: &str) { let event = Event::new_on_date(description, date); + add_event(event); +} + +fn add_with_date_time(description: &str, date: &str, time: &str) { + let event = Event::new_on_date_time(description, date, time); + add_event(event); +} + +fn add_event(event: Event) { println!("Adding event {}", event); let mut event_list = EventList::load();