From 97198afd29a8d41e5ab9f9edd5f9f71b38c9d355 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 23 Aug 2020 20:47:29 +0200 Subject: Store timestamps in UTC. For now we just hardcode that the local timezone of the site is UTC+2. Good enough for now, but something that should be configurable. Also split the date and time entries in the form again, the date refers to the start date. If the end time is before the start time, we assume it's the day after. Again, good enough for now. --- src/controllers/screening.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'src/controllers/screening.rs') diff --git a/src/controllers/screening.rs b/src/controllers/screening.rs index aca8e32..40eefd3 100644 --- a/src/controllers/screening.rs +++ b/src/controllers/screening.rs @@ -68,20 +68,30 @@ pub fn new_screening(db: db::Connection) -> Result> { pub struct NewScreeningForm { film_id: i32, room_id: i32, + date: String, start_time: String, end_time: String, } +fn parse_datetime(date: &str, time: &str) -> Result, Box> { + let dts = format!("{}T{}:00+02:00", &date, &time); + Ok(chrono::DateTime::parse_from_rfc3339(&dts)?.with_timezone(&chrono::Utc)) +} + #[post("/", format = "application/x-www-form-urlencoded", data = "")] pub fn create_screening(db: db::Connection, screening: Form) -> Result> { - let start_time = chrono::DateTime::parse_from_rfc3339(dbg!(&screening.start_time))?; - let end_time = chrono::DateTime::parse_from_rfc3339(dbg!(&screening.end_time))?; + let start_time = parse_datetime(&screening.date, &screening.start_time)?; + let mut end_time = parse_datetime(&screening.date, &screening.end_time)?; + + if end_time < start_time { + end_time = end_time + chrono::Duration::days(1); + } db.create_screening( - dbg!(screening.room_id), - dbg!(screening.film_id), - start_time.with_timezone(&chrono::Utc), - end_time.with_timezone(&chrono::Utc))?; + screening.room_id, + screening.film_id, + start_time, + end_time)?; Ok(Redirect::to("/screenings")) } -- cgit v1.2.3