baseunits_rs/time/
mod.rs

1use num::{FromPrimitive, ToPrimitive};
2
3pub use calendar_date::*;
4pub use calendar_date_time::*;
5pub use calendar_year_month::*;
6pub use day_of_month::*;
7pub use hour_of_day::*;
8pub use minute_of_hour::*;
9pub use month_of_year::*;
10pub use time_point::*;
11pub use time_unit::*;
12pub use time_of_day::*;
13
14mod calendar_date;
15mod calendar_date_time;
16mod calendar_year_month;
17mod day_of_month;
18mod duration;
19mod hour_of_day;
20mod minute_of_hour;
21mod month_of_year;
22mod time_of_day;
23mod time_point;
24mod time_unit;
25mod time_unit_conversion_factor;
26
27#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Hash)]
28pub enum DayOfWeek {
29  Sunday,
30  Monday,
31  Tuesday,
32  Wednesday,
33  Thursday,
34  Friday,
35  Saturday,
36}
37
38impl DayOfWeek {}
39
40impl ToPrimitive for DayOfWeek {
41  fn to_i64(&self) -> Option<i64> {
42    match *self {
43      DayOfWeek::Sunday => Some(1),
44      DayOfWeek::Monday => Some(2),
45      DayOfWeek::Tuesday => Some(3),
46      DayOfWeek::Wednesday => Some(4),
47      DayOfWeek::Thursday => Some(5),
48      DayOfWeek::Friday => Some(6),
49      DayOfWeek::Saturday => Some(7),
50    }
51  }
52
53  fn to_u64(&self) -> Option<u64> {
54    match *self {
55      DayOfWeek::Sunday => Some(1),
56      DayOfWeek::Monday => Some(2),
57      DayOfWeek::Tuesday => Some(3),
58      DayOfWeek::Wednesday => Some(4),
59      DayOfWeek::Thursday => Some(5),
60      DayOfWeek::Friday => Some(6),
61      DayOfWeek::Saturday => Some(7),
62    }
63  }
64}
65
66impl FromPrimitive for DayOfWeek {
67  fn from_i64(n: i64) -> Option<Self> {
68    match n {
69      1 => Some(DayOfWeek::Sunday),
70      2 => Some(DayOfWeek::Monday),
71      3 => Some(DayOfWeek::Tuesday),
72      4 => Some(DayOfWeek::Wednesday),
73      5 => Some(DayOfWeek::Thursday),
74      6 => Some(DayOfWeek::Friday),
75      7 => Some(DayOfWeek::Saturday),
76      _ => None,
77    }
78  }
79
80  fn from_u64(n: u64) -> Option<Self> {
81    match n {
82      1 => Some(DayOfWeek::Sunday),
83      2 => Some(DayOfWeek::Monday),
84      3 => Some(DayOfWeek::Tuesday),
85      4 => Some(DayOfWeek::Wednesday),
86      5 => Some(DayOfWeek::Thursday),
87      6 => Some(DayOfWeek::Friday),
88      7 => Some(DayOfWeek::Saturday),
89      _ => None,
90    }
91  }
92}
93
94#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Hash)]
95pub enum Month {
96  January,
97  February,
98  March,
99  April,
100  May,
101  June,
102  July,
103  August,
104  September,
105  October,
106  November,
107  December,
108}
109
110impl ToPrimitive for Month {
111  fn to_i64(&self) -> Option<i64> {
112    match *self {
113      Month::January => Some(1),
114      Month::February => Some(2),
115      Month::March => Some(3),
116      Month::April => Some(4),
117      Month::May => Some(5),
118      Month::June => Some(6),
119      Month::July => Some(7),
120      Month::August => Some(8),
121      Month::September => Some(9),
122      Month::October => Some(10),
123      Month::November => Some(11),
124      Month::December => Some(12),
125    }
126  }
127
128  fn to_u64(&self) -> Option<u64> {
129    match *self {
130      Month::January => Some(1),
131      Month::February => Some(2),
132      Month::March => Some(3),
133      Month::April => Some(4),
134      Month::May => Some(5),
135      Month::June => Some(6),
136      Month::July => Some(7),
137      Month::August => Some(8),
138      Month::September => Some(9),
139      Month::October => Some(10),
140      Month::November => Some(11),
141      Month::December => Some(12),
142    }
143  }
144}
145
146impl FromPrimitive for Month {
147  fn from_i64(n: i64) -> Option<Self> {
148    match n {
149      1 => Some(Month::January),
150      2 => Some(Month::February),
151      3 => Some(Month::March),
152      4 => Some(Month::April),
153      5 => Some(Month::May),
154      6 => Some(Month::June),
155      7 => Some(Month::July),
156      8 => Some(Month::August),
157      9 => Some(Month::September),
158      10 => Some(Month::October),
159      11 => Some(Month::November),
160      12 => Some(Month::December),
161      _ => None,
162    }
163  }
164
165  fn from_u64(n: u64) -> Option<Self> {
166    match n {
167      1 => Some(Month::January),
168      2 => Some(Month::February),
169      3 => Some(Month::March),
170      4 => Some(Month::April),
171      5 => Some(Month::May),
172      6 => Some(Month::June),
173      7 => Some(Month::July),
174      8 => Some(Month::August),
175      9 => Some(Month::September),
176      10 => Some(Month::October),
177      11 => Some(Month::November),
178      12 => Some(Month::December),
179      _ => None,
180    }
181  }
182}
183
184pub(crate) fn is_leap_year(year: i32) -> bool {
185  !(year % 4 != 0 || year % 100 == 0 && year % 400 != 0)
186}