stm32l4xx_hal/
datetime.rs

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