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