Browse Source

Use From from DateTime to Date and Time

master
Julio Biason 4 years ago
parent
commit
b9da2ec030
  1. 45
      src/eventlist/event.rs

45
src/eventlist/event.rs

@ -26,7 +26,6 @@ use serde_derive::Deserialize;
use serde_derive::Serialize; use serde_derive::Serialize;
use uuid::Uuid; use uuid::Uuid;
// TODO constructors
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct Date { pub struct Date {
year: i32, year: i32,
@ -34,13 +33,31 @@ pub struct Date {
day: u32, day: u32,
} }
// TODO constructors impl From<&DateTime<Local>> for Date {
fn from(origin: &DateTime<Local>) -> Date {
Date {
year: origin.year(),
month: origin.month(),
day: origin.day(),
}
}
}
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct Time { pub struct Time {
hour: u32, hour: u32,
min: u32, min: u32,
} }
impl From<&DateTime<Local>> for Time {
fn from(origin: &DateTime<Local>) -> Time {
Time {
hour: origin.hour(),
min: origin.minute(),
}
}
}
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "due", content = "datetime")] #[serde(tag = "due", content = "datetime")]
pub enum EventDateType { pub enum EventDateType {
@ -83,17 +100,15 @@ fn uuid() -> String {
} }
impl Event { impl Event {
// TODO result this
pub fn new_on_date(description: &str, date: &str) -> Self { pub fn new_on_date(description: &str, date: &str) -> Self {
let fake_datetime = format!("{} 00:00:00", date); let fake_datetime = format!("{} 00:00:00", date);
if let Ok(dt) = Utc.datetime_from_str(&fake_datetime, "%Y-%m-%d %H:%M:%S") { if let Ok(dt) = Local.datetime_from_str(&fake_datetime, "%Y-%m-%d %H:%M:%S") {
// TODO turn format into static
Self { Self {
id: uuid(), id: uuid(),
description: description.into(), description: description.into(),
due: EventDateType::AllDay(Date { due: EventDateType::AllDay(Date::from(&dt)),
year: dt.year(),
month: dt.month(),
day: dt.day(),
}),
} }
} else { } else {
panic!("Failed to parse the date"); panic!("Failed to parse the date");
@ -102,21 +117,11 @@ impl Event {
pub fn new_on_date_time(description: &str, date: &str, time: &str) -> Self { pub fn new_on_date_time(description: &str, date: &str, time: &str) -> Self {
let fake_datetime = format!("{} {}:00", date, time); let fake_datetime = format!("{} {}:00", date, time);
if let Ok(dt) = Utc.datetime_from_str(&fake_datetime, "%Y-%m-%d %H:%M:%S") { if let Ok(dt) = Local.datetime_from_str(&fake_datetime, "%Y-%m-%d %H:%M:%S") {
Self { Self {
id: uuid(), id: uuid(),
description: description.into(), description: description.into(),
due: EventDateType::AtTime( due: EventDateType::AtTime(Date::from(&dt), Time::from(&dt)),
Date {
year: dt.year(),
month: dt.month(),
day: dt.day(),
},
Time {
hour: dt.hour(),
min: dt.minute(),
},
),
} }
} else { } else {
panic!("Failed to parse the date"); panic!("Failed to parse the date");

Loading…
Cancel
Save