Browse Source

Deal with times

master
Julio Biason 4 years ago
parent
commit
b27c370a45
  1. 6
      Cargo.toml
  2. 6
      README.md
  3. 56
      src/eventlist/event.rs
  4. 13
      src/main.rs

6
Cargo.toml

@ -6,11 +6,11 @@ authors = ["Julio Biason <julio.biason@gmail.com>"]
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"

6
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

56
src/eventlist/event.rs

@ -46,11 +46,13 @@ pub enum EventDateType {
AtTime(Date, Time),
}
impl From<&EventDateType> for DateTime<Utc> {
impl From<&EventDateType> for DateTime<Local> {
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<String> {
let now = Utc::now();
let to: DateTime<Utc> = (&self.due).into();
let now = Local::now();
let to: DateTime<Local> = (&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,
}
}
}

13
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();

Loading…
Cancel
Save