gitlab_time_report/
chrono_extensions.rs1use chrono::TimeDelta;
4
5pub trait TimeDeltaExt {
7 fn to_hm_string(&self) -> String;
8 fn total_hours(&self) -> f32;
9}
10
11impl TimeDeltaExt for TimeDelta {
12 fn to_hm_string(&self) -> String {
21 let hours = self.num_hours();
23 let minutes = self.num_minutes() % 60;
24 format!("{hours:02}h {minutes:02}m")
25 }
26
27 fn total_hours(&self) -> f32 {
29 self.as_seconds_f32() / 3600.0
30 }
31}
32
33#[cfg(test)]
34mod tests {
35 use super::*;
36
37 #[test]
38 fn test_to_readable_string() {
39 let delta = TimeDelta::hours(1) + TimeDelta::minutes(45) + TimeDelta::seconds(3);
40 assert_eq!(delta.to_hm_string(), "01h 45m");
41 }
42
43 #[test]
44 #[expect(clippy::float_cmp)]
45 fn test_total_hours() {
46 let delta = TimeDelta::hours(5) + TimeDelta::minutes(45);
47 assert_eq!(delta.total_hours(), 5.75);
48 }
49}