1use super::{time::Time, Error};
16
17pub fn time_from_ymdhms_utc(
18 year: u64, month: u64, day_of_month: u64, hours: u64, minutes: u64, seconds: u64,
19) -> Result<Time, Error> {
20 let days_before_year_since_unix_epoch = days_before_year_since_unix_epoch(year)?;
21
22 const JAN: u64 = 31;
23 let feb = days_in_feb(year);
24 const MAR: u64 = 31;
25 const APR: u64 = 30;
26 const MAY: u64 = 31;
27 const JUN: u64 = 30;
28 const JUL: u64 = 31;
29 const AUG: u64 = 31;
30 const SEP: u64 = 30;
31 const OCT: u64 = 31;
32 const NOV: u64 = 30;
33 let days_before_month_in_year = match month {
34 1 => 0,
35 2 => JAN,
36 3 => JAN + feb,
37 4 => JAN + feb + MAR,
38 5 => JAN + feb + MAR + APR,
39 6 => JAN + feb + MAR + APR + MAY,
40 7 => JAN + feb + MAR + APR + MAY + JUN,
41 8 => JAN + feb + MAR + APR + MAY + JUN + JUL,
42 9 => JAN + feb + MAR + APR + MAY + JUN + JUL + AUG,
43 10 => JAN + feb + MAR + APR + MAY + JUN + JUL + AUG + SEP,
44 11 => JAN + feb + MAR + APR + MAY + JUN + JUL + AUG + SEP + OCT,
45 12 => JAN + feb + MAR + APR + MAY + JUN + JUL + AUG + SEP + OCT + NOV,
46 _ => unreachable!(), };
48
49 let days_before =
50 days_before_year_since_unix_epoch + days_before_month_in_year + day_of_month - 1;
51
52 let seconds_since_unix_epoch =
53 (days_before * 24 * 60 * 60) + (hours * 60 * 60) + (minutes * 60) + seconds;
54
55 Ok(Time::from_seconds_since_unix_epoch(
56 seconds_since_unix_epoch,
57 ))
58}
59
60fn days_before_year_since_unix_epoch(year: u64) -> Result<u64, Error> {
61 if year < 1970 {
65 return Err(Error::BadDERTime);
66 }
67 let days_before_year_ad = days_before_year_ad(year);
68 debug_assert!(days_before_year_ad >= DAYS_BEFORE_UNIX_EPOCH_AD);
69 Ok(days_before_year_ad - DAYS_BEFORE_UNIX_EPOCH_AD)
70}
71
72fn days_before_year_ad(year: u64) -> u64 {
73 ((year - 1) * 365)
74 + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400) }
78
79pub fn days_in_month(year: u64, month: u64) -> u64 {
80 match month {
81 1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
82 4 | 6 | 9 | 11 => 30,
83 2 => days_in_feb(year),
84 _ => unreachable!(), }
86}
87
88fn days_in_feb(year: u64) -> u64 {
89 if (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) {
90 29
91 } else {
92 28
93 }
94}
95
96const DAYS_BEFORE_UNIX_EPOCH_AD: u64 = 719162;
97
98#[cfg(test)]
99mod tests {
100 #[test]
101 fn test_days_before_unix_epoch() {
102 use super::{days_before_year_ad, DAYS_BEFORE_UNIX_EPOCH_AD};
103 assert_eq!(DAYS_BEFORE_UNIX_EPOCH_AD, days_before_year_ad(1970));
104 }
105
106 #[test]
107 fn test_days_in_month() {
108 use super::days_in_month;
109 assert_eq!(days_in_month(2017, 1), 31);
110 assert_eq!(days_in_month(2017, 2), 28);
111 assert_eq!(days_in_month(2017, 3), 31);
112 assert_eq!(days_in_month(2017, 4), 30);
113 assert_eq!(days_in_month(2017, 5), 31);
114 assert_eq!(days_in_month(2017, 6), 30);
115 assert_eq!(days_in_month(2017, 7), 31);
116 assert_eq!(days_in_month(2017, 8), 31);
117 assert_eq!(days_in_month(2017, 9), 30);
118 assert_eq!(days_in_month(2017, 10), 31);
119 assert_eq!(days_in_month(2017, 11), 30);
120 assert_eq!(days_in_month(2017, 12), 31);
121
122 assert_eq!(days_in_month(2000, 2), 29);
124 assert_eq!(days_in_month(2004, 2), 29);
125 assert_eq!(days_in_month(2016, 2), 29);
126 assert_eq!(days_in_month(2100, 2), 28);
127 }
128
129 #[test]
130 fn test_time_from_ymdhms_utc() {
131 use super::{time_from_ymdhms_utc, Time};
132
133 assert_eq!(
135 Time::from_seconds_since_unix_epoch(1483228799),
136 time_from_ymdhms_utc(2016, 12, 31, 23, 59, 59).unwrap()
137 );
138 assert_eq!(
139 Time::from_seconds_since_unix_epoch(1483228800),
140 time_from_ymdhms_utc(2017, 1, 1, 0, 0, 0).unwrap()
141 );
142
143 assert_eq!(
145 Time::from_seconds_since_unix_epoch(1492449162),
146 time_from_ymdhms_utc(2017, 4, 17, 17, 12, 42).unwrap()
147 );
148
149 assert_eq!(
151 Time::from_seconds_since_unix_epoch(1460913162),
152 time_from_ymdhms_utc(2016, 4, 17, 17, 12, 42).unwrap()
153 );
154 }
155}