|
| 1 | +/// Date and timer units & helper functions |
| 2 | +
|
| 3 | +/// Seconds |
| 4 | +#[derive(Clone, Copy, Debug)] |
| 5 | +pub struct Second(pub u32); |
| 6 | + |
| 7 | +/// Minutes |
| 8 | +#[derive(Clone, Copy, Debug)] |
| 9 | +pub struct Minute(pub u32); |
| 10 | + |
| 11 | +/// Hours |
| 12 | +#[derive(Clone, Copy, Debug)] |
| 13 | +pub struct Hour(pub u32); |
| 14 | + |
| 15 | +/// Day (1-7) |
| 16 | +#[derive(Clone, Copy, Debug)] |
| 17 | +pub struct Day(pub u32); |
| 18 | + |
| 19 | +/// Date (1-31) |
| 20 | +#[derive(Clone, Copy, Debug)] |
| 21 | +pub struct DateInMonth(pub u32); |
| 22 | + |
| 23 | +/// Week (1-52) |
| 24 | +#[derive(Clone, Copy, Debug)] |
| 25 | +pub struct Week(pub u32); |
| 26 | + |
| 27 | +/// Month (1-12) |
| 28 | +#[derive(Clone, Copy, Debug)] |
| 29 | +pub struct Month(pub u32); |
| 30 | + |
| 31 | +/// Year |
| 32 | +#[derive(Clone, Copy, Debug)] |
| 33 | +pub struct Year(pub u32); |
| 34 | + |
| 35 | +/// Extension trait that adds convenience methods to the `u32` type |
| 36 | +pub trait U32Ext { |
| 37 | + /// Seconds |
| 38 | + fn seconds(self) -> Second; |
| 39 | + /// Minutes |
| 40 | + fn minutes(self) -> Minute; |
| 41 | + /// Hours |
| 42 | + fn hours(self) -> Hour; |
| 43 | + /// Day |
| 44 | + fn day(self) -> Day; |
| 45 | + /// Seconds |
| 46 | + fn date(self) -> DateInMonth; |
| 47 | + /// Month |
| 48 | + fn month(self) -> Month; |
| 49 | + /// Year |
| 50 | + fn year(self) -> Year; |
| 51 | +} |
| 52 | + |
| 53 | +impl U32Ext for u32 { |
| 54 | + fn seconds(self) -> Second { |
| 55 | + Second(self) |
| 56 | + } |
| 57 | + |
| 58 | + fn minutes(self) -> Minute { |
| 59 | + Minute(self) |
| 60 | + } |
| 61 | + |
| 62 | + fn hours(self) -> Hour { |
| 63 | + Hour(self) |
| 64 | + } |
| 65 | + |
| 66 | + fn day(self) -> Day { |
| 67 | + Day(self) |
| 68 | + } |
| 69 | + |
| 70 | + fn date(self) -> DateInMonth { |
| 71 | + DateInMonth(self) |
| 72 | + } |
| 73 | + |
| 74 | + fn month(self) -> Month { |
| 75 | + Month(self) |
| 76 | + } |
| 77 | + |
| 78 | + fn year(self) -> Year { |
| 79 | + Year(self) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +#[derive(Clone,Copy,Debug)] |
| 84 | +pub struct Time { |
| 85 | + pub hours: u32, |
| 86 | + pub minutes: u32, |
| 87 | + pub seconds: u32, |
| 88 | + pub daylight_savings: bool |
| 89 | +} |
| 90 | + |
| 91 | +impl Time { |
| 92 | + pub fn new(hours: Hour, minutes: Minute, seconds: Second, daylight_savings: bool) -> Self { |
| 93 | + Self { |
| 94 | + hours: hours.0, |
| 95 | + minutes: minutes.0, |
| 96 | + seconds: seconds.0, |
| 97 | + daylight_savings: daylight_savings |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[derive(Clone,Copy, Debug)] |
| 103 | +pub struct Date { |
| 104 | + pub day: u32, |
| 105 | + pub date: u32, |
| 106 | + pub month: u32, |
| 107 | + pub year: u32, |
| 108 | +} |
| 109 | + |
| 110 | +impl Date { |
| 111 | + pub fn new(day: Day, date: DateInMonth, month: Month, year: Year) -> Self { |
| 112 | + Self { |
| 113 | + day: day.0, |
| 114 | + date: date.0, |
| 115 | + month: month.0, |
| 116 | + year: year.0 |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +impl Into<Second> for Minute { |
| 122 | + fn into(self) -> Second { |
| 123 | + Second(self.0 * 60) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +impl Into<Second> for Hour { |
| 128 | + fn into(self) -> Second { |
| 129 | + Second(self.0 * 3600) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +macro_rules! impl_from_struct { |
| 134 | + ($( |
| 135 | + $type:ident: [ $($to:ident),+ ], |
| 136 | + )+) => { |
| 137 | + $( |
| 138 | + $( |
| 139 | + impl From <$type> for $to { |
| 140 | + fn from(inner: $type) -> $to { |
| 141 | + inner.0 as $to |
| 142 | + } |
| 143 | + } |
| 144 | + )+ |
| 145 | + )+ |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +macro_rules! impl_to_struct { |
| 150 | + ($( |
| 151 | + $type:ident: [ $($to:ident),+ ], |
| 152 | + )+) => { |
| 153 | + $( |
| 154 | + $( |
| 155 | + impl From <$type> for $to { |
| 156 | + fn from(inner: $type) -> $to { |
| 157 | + $to(inner as u32) |
| 158 | + } |
| 159 | + } |
| 160 | + )+ |
| 161 | + )+ |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +impl_from_struct!( |
| 166 | + Hour: [u32, u16, u8], |
| 167 | + Second: [u32, u16, u8], |
| 168 | + Minute: [u32, u16, u8], |
| 169 | + Day: [u32, u16, u8], |
| 170 | + DateInMonth: [u32, u16, u8], |
| 171 | + Month: [u32, u16, u8], |
| 172 | + Year: [u32, u16, u8], |
| 173 | +); |
| 174 | + |
| 175 | +impl_to_struct!( |
| 176 | + u32: [Hour, Minute, Second, Day, DateInMonth, Month, Year], |
| 177 | + u16: [Hour, Minute, Second, Day, DateInMonth, Month, Year], |
| 178 | + u8: [Hour, Minute, Second, Day, DateInMonth, Month, Year], |
| 179 | +); |
0 commit comments