2022-07-30 09:46:22 -05:00
|
|
|
use chrono::{DateTime, Utc, Duration};
|
2021-07-26 20:32:13 -05:00
|
|
|
use serde::Serialize;
|
2021-06-21 11:12:30 -05:00
|
|
|
|
2022-08-29 22:41:50 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
2021-06-21 11:12:30 -05:00
|
|
|
pub struct Entry {
|
|
|
|
pub id: u64,
|
2021-06-30 18:51:02 -05:00
|
|
|
pub note: Option<String>,
|
2021-06-24 00:07:52 -05:00
|
|
|
pub start: DateTime<Utc>,
|
|
|
|
pub end: Option<DateTime<Utc>>,
|
2021-06-21 11:12:30 -05:00
|
|
|
pub sheet: String,
|
|
|
|
}
|
|
|
|
|
2021-06-21 17:38:51 -05:00
|
|
|
#[derive(Debug)]
|
2021-06-21 11:12:30 -05:00
|
|
|
pub struct Meta {
|
|
|
|
pub id: u64,
|
|
|
|
pub key: String,
|
|
|
|
pub value: String,
|
|
|
|
}
|
2021-06-21 19:54:10 -05:00
|
|
|
|
|
|
|
impl Entry {
|
|
|
|
#[cfg(test)]
|
2021-06-24 00:07:52 -05:00
|
|
|
pub fn new_sample(id: u64, start: DateTime<Utc>, end: Option<DateTime<Utc>>) -> Entry {
|
2021-06-21 19:54:10 -05:00
|
|
|
Entry {
|
|
|
|
id,
|
2021-06-30 18:51:02 -05:00
|
|
|
note: Some(format!("entry {}", id)),
|
2021-06-21 19:54:10 -05:00
|
|
|
start, end,
|
|
|
|
sheet: "default".into(),
|
|
|
|
}
|
|
|
|
}
|
2022-07-30 09:46:22 -05:00
|
|
|
|
2022-11-25 09:03:40 -06:00
|
|
|
pub fn with_sheet(self, sheet: &str) -> Entry {
|
|
|
|
Entry {
|
|
|
|
sheet: sheet.into(),
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-30 09:46:22 -05:00
|
|
|
pub fn timespan(&self) -> Option<Duration> {
|
|
|
|
self.end.map(|e| e - self.start)
|
|
|
|
}
|
2022-08-28 15:48:47 -05:00
|
|
|
|
|
|
|
// returns the number of hours of this entry as decimal. If entry is
|
|
|
|
// unfinished return its elapsed time so far.
|
|
|
|
pub fn hours(&self, now: DateTime<Utc>) -> f64 {
|
|
|
|
let d = self.end.unwrap_or(now) - self.start;
|
|
|
|
|
|
|
|
d.num_hours() as f64 + (d.num_minutes() % 60) as f64 / 60.0 + (d.num_seconds() % 60) as f64 / 3600.0
|
|
|
|
}
|
2021-06-21 19:54:10 -05:00
|
|
|
}
|