pass first output test
This commit is contained in:
parent
4d48386777
commit
be406d9211
|
@ -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),
|
||||
];
|
||||
|
||||
|
|
Loading…
Reference in New Issue