11/// Date and timer units & helper functions
22
33/// Seconds
4- #[ derive( Clone , Copy ) ]
5- pub struct Seconds ( pub u32 ) ;
4+ #[ derive( Clone , Copy , Debug ) ]
5+ pub struct Second ( pub u32 ) ;
66
77/// Minutes
8- #[ derive( Clone , Copy ) ]
9- pub struct Minutes ( pub u32 ) ;
8+ #[ derive( Clone , Copy , Debug ) ]
9+ pub struct Minute ( pub u32 ) ;
1010
1111/// Hours
12- #[ derive( Clone , Copy ) ]
13- pub struct Hours ( pub u32 ) ;
12+ #[ derive( Clone , Copy , Debug ) ]
13+ pub struct Hour ( pub u32 ) ;
1414
1515/// Day (1-7)
16- #[ derive( Clone , Copy ) ]
16+ #[ derive( Clone , Copy , Debug ) ]
1717pub struct Day ( pub u32 ) ;
1818
1919/// Date (1-31)
20- #[ derive( Clone , Copy ) ]
21- pub struct Date ( pub u32 ) ;
20+ #[ derive( Clone , Copy , Debug ) ]
21+ pub struct DateInMonth ( pub u32 ) ;
2222
2323/// Week (1-52)
24- #[ derive( Clone , Copy ) ]
24+ #[ derive( Clone , Copy , Debug ) ]
2525pub struct Week ( pub u32 ) ;
2626
2727/// Month (1-12)
28- #[ derive( Clone , Copy ) ]
28+ #[ derive( Clone , Copy , Debug ) ]
2929pub struct Month ( pub u32 ) ;
3030
3131/// Year
32- #[ derive( Clone , Copy ) ]
32+ #[ derive( Clone , Copy , Debug ) ]
3333pub struct Year ( pub u32 ) ;
3434
3535/// Extension trait that adds convenience methods to the `u32` type
3636pub trait U32Ext {
3737 /// Seconds
38- fn seconds ( self ) -> Seconds ;
38+ fn seconds ( self ) -> Second ;
3939 /// Minutes
40- fn minutes ( self ) -> Minutes ;
40+ fn minutes ( self ) -> Minute ;
4141 /// Hours
42- fn hours ( self ) -> Hours ;
42+ fn hours ( self ) -> Hour ;
4343 /// Day
4444 fn day ( self ) -> Day ;
4545 /// Seconds
46- fn date ( self ) -> Date ;
46+ fn date ( self ) -> DateInMonth ;
4747 /// Month
4848 fn month ( self ) -> Month ;
4949 /// Year
5050 fn year ( self ) -> Year ;
5151}
5252
5353impl U32Ext for u32 {
54- fn seconds ( self ) -> Seconds {
55- Seconds ( self )
54+ fn seconds ( self ) -> Second {
55+ Second ( self )
5656 }
5757
58- fn minutes ( self ) -> Minutes {
59- Minutes ( self )
58+ fn minutes ( self ) -> Minute {
59+ Minute ( self )
6060 }
6161
62- fn hours ( self ) -> Hours {
63- Hours ( self )
62+ fn hours ( self ) -> Hour {
63+ Hour ( self )
6464 }
6565
6666 fn day ( self ) -> Day {
6767 Day ( self )
6868 }
6969
70- fn date ( self ) -> Date {
71- Date ( self )
70+ fn date ( self ) -> DateInMonth {
71+ DateInMonth ( self )
7272 }
7373
7474 fn month ( self ) -> Month {
@@ -80,32 +80,100 @@ impl U32Ext for u32 {
8080 }
8181}
8282
83- impl Into < Seconds > for Minutes {
84- fn into ( self ) -> Seconds {
85- Seconds ( self . 0 * 60 )
83+ #[ derive( Clone , Copy , Debug ) ]
84+ pub struct Time {
85+ pub hours : Hour ,
86+ pub minutes : Minute ,
87+ pub seconds : Second ,
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,
95+ minutes : minutes,
96+ seconds : seconds,
97+ daylight_savings : daylight_savings
98+ }
99+ }
100+ }
101+
102+ #[ derive( Clone , Copy , Debug ) ]
103+ pub struct Date {
104+ pub day : Day ,
105+ pub date : DateInMonth ,
106+ pub month : Month ,
107+ pub year : Year ,
108+ }
109+
110+ impl Date {
111+ pub fn new ( day : Day , date : DateInMonth , month : Month , year : Year ) -> Self {
112+ Self {
113+ day : day,
114+ date : date,
115+ month : month,
116+ year : year
117+ }
86118 }
87119}
88120
89- impl Into < Seconds > for Hours {
90- fn into ( self ) -> Seconds {
91- Seconds ( self . 0 * 3600 )
121+ impl Into < Second > for Minute {
122+ fn into ( self ) -> Second {
123+ Second ( self . 0 * 60 )
92124 }
93125}
94126
95- impl From < u32 > for Seconds {
96- fn from ( inner : u32 ) -> Seconds {
97- Seconds ( inner )
127+ impl Into < Second > for Hour {
128+ fn into ( self ) -> Second {
129+ Second ( self . 0 * 3600 )
98130 }
99131}
100132
101- impl From < u32 > for Minutes {
102- fn from ( inner : u32 ) -> Minutes {
103- Minutes ( inner)
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+ ) +
104146 }
105147}
106148
107- impl From < u32 > for Hours {
108- fn from ( inner : u32 ) -> Hours {
109- Hours ( inner)
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+ ) +
110162 }
111163}
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