Use Time instead of DateTime

This commit is contained in:
kageru 2020-04-20 10:27:59 +02:00
parent 129e890670
commit 6075a4369f

View File

@ -7,7 +7,7 @@ use std::default;
use std::fmt; use std::fmt;
use std::fs; use std::fs;
use std::io; use std::io;
use time::{Duration, OffsetDateTime}; use time::{Duration, OffsetDateTime, Time};
pub struct TimeSheet { pub struct TimeSheet {
pub times: Vec<TimePoint>, pub times: Vec<TimePoint>,
@ -19,28 +19,21 @@ pub struct TimeSheet {
#[derive(Serialize, Deserialize, Clone, Debug)] #[derive(Serialize, Deserialize, Clone, Debug)]
pub struct TimePoint { pub struct TimePoint {
text: String, text: String,
time: OffsetDateTime, time: Time,
} }
impl TimePoint { impl TimePoint {
pub fn new(text: &str) -> Self { pub fn new(text: &str) -> Self {
Self { Self {
text: String::from(text), text: String::from(text),
time: OffsetDateTime::now_local(), time: OffsetDateTime::now_local().time(),
} }
} }
} }
impl fmt::Display for TimePoint { impl fmt::Display for TimePoint {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!( write!(f, "[{}] {}", self.time.format("%H:%M"), self.text)
f,
"[{}] {}",
self.time
.to_offset(time::UtcOffset::current_local_offset())
.format("%H:%M"),
self.text
)
} }
} }
@ -80,6 +73,9 @@ impl TimeSheet {
.iter() .iter()
.tuple_windows() .tuple_windows()
.map(|(prev, next)| (prev.text.clone(), next.time - prev.time)) .map(|(prev, next)| (prev.text.clone(), next.time - prev.time))
// Fold into a map to group by description.
// I use a BTreeMap because I need a stable output order for the iterator
// (otherwise the summary list will jump around on every input).
.fold(collections::BTreeMap::new(), |mut map, (text, duration)| { .fold(collections::BTreeMap::new(), |mut map, (text, duration)| {
*map.entry(text).or_insert_with(Duration::zero) += duration; *map.entry(text).or_insert_with(Duration::zero) += duration;
map map