baseunits_rs/time/
hour_of_day.rs1use std::ops::{Add, Sub};
2
3#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Hash)]
4pub struct HourOfDay(u32);
5
6impl ToString for HourOfDay {
7 fn to_string(&self) -> String {
8 format!("{:02}", self.0)
9 }
10}
11
12impl Add for HourOfDay {
13 type Output = Self;
14
15 fn add(self, rhs: Self) -> Self::Output {
16 HourOfDay::new(self.0 + rhs.0)
17 }
18}
19
20impl Sub for HourOfDay {
21 type Output = Self;
22
23 fn sub(self, rhs: Self) -> Self::Output {
24 HourOfDay::new(self.0 - rhs.0)
25 }
26}
27
28impl HourOfDay {
29 pub const MIN: u32 = 0;
30 pub const MAX: u32 = 23;
31
32 pub fn new(value: u32) -> Self {
33 if value > HourOfDay::MAX {
34 panic!(
35 "Illegal value for 24 hour : {:}, please use a value between 0 and 23",
36 value
37 )
38 }
39 Self(value)
40 }
41
42 pub fn is_after(&self, other: &Self) -> bool {
43 !self.is_before(other) && self != other
44 }
45
46 pub fn is_before(&self, other: &Self) -> bool {
47 self.0 < other.0
48 }
49}