gitlab_time_report/
chrono_extensions.rs

1//! Convenience functions for the types provided by the [`chrono`] crate.
2
3use chrono::TimeDelta;
4
5/// Functions to simplify the handling of [`TimeDelta`]
6pub trait TimeDeltaExt {
7    fn to_hm_string(&self) -> String;
8    fn total_hours(&self) -> f32;
9}
10
11impl TimeDeltaExt for TimeDelta {
12    /// Turn a `TimeDelta` into a string of the format `"00h 00m"`.
13    /// # Example
14    /// ```
15    /// # use chrono::TimeDelta;
16    /// # use gitlab_time_report::TimeDeltaExt;
17    /// let delta = TimeDelta::hours(1) + TimeDelta::minutes(2) + TimeDelta::seconds(3);
18    /// assert_eq!(delta.to_hm_string(), "01h 02m");
19    /// ```
20    fn to_hm_string(&self) -> String {
21        // TimeDelta doesn't have a format() function like DateTime, so we have to do it manually
22        let hours = self.num_hours();
23        let minutes = self.num_minutes() % 60;
24        format!("{hours:02}h {minutes:02}m")
25    }
26
27    /// Get the total hours, minutes and seconds of a `TimeDelta` as a `String`.
28    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}