From be406d9211e2953b5a6f22c8904b7ed76203875b Mon Sep 17 00:00:00 2001 From: Abraham Toriz Date: Thu, 24 Jun 2021 00:28:10 -0500 Subject: [PATCH] pass first output test --- src/formatters.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/formatters.rs b/src/formatters.rs index 54d9cf6..64b99d2 100644 --- a/src/formatters.rs +++ b/src/formatters.rs @@ -3,11 +3,15 @@ use std::io::Write; use serde::{Serialize, Deserialize}; use itertools::Itertools; -use chrono::{DateTime, Utc, Offset, TimeZone}; +use chrono::{DateTime, Utc, Offset, TimeZone, Duration}; use crate::error; use crate::models::Entry; +fn format_duration(dur: Duration) -> String { + format!("{}:{:02}:{:02}", dur.num_hours(), dur.num_minutes() % 60, dur.num_seconds() % 60) +} + #[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] pub enum Formatter { @@ -46,14 +50,18 @@ impl Formatter { writeln!(out, "Timesheet: {}", key)?; writeln!(out, " Day Start End Duration Notes")?; - let by_day = group.group_by(|e| fixed_offset.from_utc_datetime(&e.start.naive_utc()).date()); + let entries_by_date = group.group_by(|e| fixed_offset.from_utc_datetime(&e.start.naive_utc()).date()); + let mut total = Duration::seconds(0); + + for (date, entries) in entries_by_date.into_iter() { + let mut daily = Duration::seconds(0); - for (date, entries) in by_day.into_iter() { for (i,entry) in entries.into_iter().enumerate() { let start = fixed_offset.from_utc_datetime(&entry.start.naive_utc()).time().to_string(); let end = entry.end.map(|t| fixed_offset.from_utc_datetime(&t.naive_utc()).time().to_string()).unwrap_or(" ".into()); let duration = entry.end.unwrap_or(now) - entry.start; - let duration = format!("{}:{:02}:{:02}", duration.num_hours(), duration.num_minutes() % 60, duration.num_seconds() & 60); + daily = daily + duration; + let duration = format_duration(duration); if i == 0 { let date = date.format("%a %b %d, %Y").to_string(); @@ -71,7 +79,14 @@ impl Formatter { )?; } } + + total = total + daily; + + writeln!(out, " {}", format_duration(daily))?; } + + writeln!(out, " -----------------------------------------------------------")?; + writeln!(out, " Total {}", format_duration(total))?; } Ok(()) @@ -115,8 +130,8 @@ mod tests { let mut output = Vec::new(); let entries = vec![ Entry::new_sample(1, Utc.ymd(2008, 10, 3).and_hms(12, 0, 0), Some(Utc.ymd(2008, 10, 3).and_hms(14, 0, 0))), - Entry::new_sample(2, Utc.ymd(2008, 10, 3).and_hms(12, 0, 0), Some(Utc.ymd(2008, 10, 3).and_hms(14, 0, 0))), - Entry::new_sample(3, Utc.ymd(2008, 10, 5).and_hms(16, 0, 0), Some(Utc.ymd(2008, 10, 3).and_hms(18, 0, 0))), + Entry::new_sample(2, Utc.ymd(2008, 10, 3).and_hms(16, 0, 0), Some(Utc.ymd(2008, 10, 3).and_hms(18, 0, 0))), + Entry::new_sample(3, Utc.ymd(2008, 10, 5).and_hms(16, 0, 0), Some(Utc.ymd(2008, 10, 5).and_hms(18, 0, 0))), Entry::new_sample(4, Utc.ymd(2008, 10, 5).and_hms(18, 0, 0), None), ];