1 // Copyright 2015-2016 Brian Smith.
2 //
3 // Permission to use, copy, modify, and/or distribute this software for any
4 // purpose with or without fee is hereby granted, provided that the above
5 // copyright notice and this permission notice appear in all copies.
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
8 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
10 // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 
15 use super::{time::Time, Error};
16 
time_from_ymdhms_utc( year: u64, month: u64, day_of_month: u64, hours: u64, minutes: u64, seconds: u64, ) -> Result<Time, Error>17 pub fn time_from_ymdhms_utc(
18     year: u64,
19     month: u64,
20     day_of_month: u64,
21     hours: u64,
22     minutes: u64,
23     seconds: u64,
24 ) -> Result<Time, Error> {
25     let days_before_year_since_unix_epoch = days_before_year_since_unix_epoch(year)?;
26 
27     const JAN: u64 = 31;
28     let feb = days_in_feb(year);
29     const MAR: u64 = 31;
30     const APR: u64 = 30;
31     const MAY: u64 = 31;
32     const JUN: u64 = 30;
33     const JUL: u64 = 31;
34     const AUG: u64 = 31;
35     const SEP: u64 = 30;
36     const OCT: u64 = 31;
37     const NOV: u64 = 30;
38     let days_before_month_in_year = match month {
39         1 => 0,
40         2 => JAN,
41         3 => JAN + feb,
42         4 => JAN + feb + MAR,
43         5 => JAN + feb + MAR + APR,
44         6 => JAN + feb + MAR + APR + MAY,
45         7 => JAN + feb + MAR + APR + MAY + JUN,
46         8 => JAN + feb + MAR + APR + MAY + JUN + JUL,
47         9 => JAN + feb + MAR + APR + MAY + JUN + JUL + AUG,
48         10 => JAN + feb + MAR + APR + MAY + JUN + JUL + AUG + SEP,
49         11 => JAN + feb + MAR + APR + MAY + JUN + JUL + AUG + SEP + OCT,
50         12 => JAN + feb + MAR + APR + MAY + JUN + JUL + AUG + SEP + OCT + NOV,
51         _ => unreachable!(), // `read_two_digits` already bounds-checked it.
52     };
53 
54     let days_before =
55         days_before_year_since_unix_epoch + days_before_month_in_year + day_of_month - 1;
56 
57     let seconds_since_unix_epoch =
58         (days_before * 24 * 60 * 60) + (hours * 60 * 60) + (minutes * 60) + seconds;
59 
60     Ok(Time::from_seconds_since_unix_epoch(
61         seconds_since_unix_epoch,
62     ))
63 }
64 
days_before_year_since_unix_epoch(year: u64) -> Result<u64, Error>65 fn days_before_year_since_unix_epoch(year: u64) -> Result<u64, Error> {
66     // We don't support dates before January 1, 1970 because that is the
67     // Unix epoch. It is likely that other software won't deal well with
68     // certificates that have dates before the epoch.
69     if year < 1970 {
70         return Err(Error::BadDerTime);
71     }
72     let days_before_year_ad = days_before_year_ad(year);
73     debug_assert!(days_before_year_ad >= DAYS_BEFORE_UNIX_EPOCH_AD);
74     Ok(days_before_year_ad - DAYS_BEFORE_UNIX_EPOCH_AD)
75 }
76 
days_before_year_ad(year: u64) -> u6477 fn days_before_year_ad(year: u64) -> u64 {
78     ((year - 1) * 365)
79         + ((year - 1) / 4)    // leap years are every 4 years,
80         - ((year - 1) / 100)  // except years divisible by 100,
81         + ((year - 1) / 400) // except years divisible by 400.
82 }
83 
days_in_month(year: u64, month: u64) -> u6484 pub fn days_in_month(year: u64, month: u64) -> u64 {
85     match month {
86         1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
87         4 | 6 | 9 | 11 => 30,
88         2 => days_in_feb(year),
89         _ => unreachable!(), // `read_two_digits` already bounds-checked it.
90     }
91 }
92 
days_in_feb(year: u64) -> u6493 fn days_in_feb(year: u64) -> u64 {
94     if (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) {
95         29
96     } else {
97         28
98     }
99 }
100 
101 #[allow(clippy::unreadable_literal)] // TODO: Make this clear.
102 const DAYS_BEFORE_UNIX_EPOCH_AD: u64 = 719162;
103 
104 #[cfg(test)]
105 mod tests {
106     #[test]
test_days_before_unix_epoch()107     fn test_days_before_unix_epoch() {
108         use super::{days_before_year_ad, DAYS_BEFORE_UNIX_EPOCH_AD};
109         assert_eq!(DAYS_BEFORE_UNIX_EPOCH_AD, days_before_year_ad(1970));
110     }
111 
112     #[test]
test_days_in_month()113     fn test_days_in_month() {
114         use super::days_in_month;
115         assert_eq!(days_in_month(2017, 1), 31);
116         assert_eq!(days_in_month(2017, 2), 28);
117         assert_eq!(days_in_month(2017, 3), 31);
118         assert_eq!(days_in_month(2017, 4), 30);
119         assert_eq!(days_in_month(2017, 5), 31);
120         assert_eq!(days_in_month(2017, 6), 30);
121         assert_eq!(days_in_month(2017, 7), 31);
122         assert_eq!(days_in_month(2017, 8), 31);
123         assert_eq!(days_in_month(2017, 9), 30);
124         assert_eq!(days_in_month(2017, 10), 31);
125         assert_eq!(days_in_month(2017, 11), 30);
126         assert_eq!(days_in_month(2017, 12), 31);
127 
128         // leap cases
129         assert_eq!(days_in_month(2000, 2), 29);
130         assert_eq!(days_in_month(2004, 2), 29);
131         assert_eq!(days_in_month(2016, 2), 29);
132         assert_eq!(days_in_month(2100, 2), 28);
133     }
134 
135     #[allow(clippy::unreadable_literal)] // TODO: Make this clear.
136     #[test]
test_time_from_ymdhms_utc()137     fn test_time_from_ymdhms_utc() {
138         use super::{time_from_ymdhms_utc, Time};
139 
140         // year boundary
141         assert_eq!(
142             Time::from_seconds_since_unix_epoch(1483228799),
143             time_from_ymdhms_utc(2016, 12, 31, 23, 59, 59).unwrap()
144         );
145         assert_eq!(
146             Time::from_seconds_since_unix_epoch(1483228800),
147             time_from_ymdhms_utc(2017, 1, 1, 0, 0, 0).unwrap()
148         );
149 
150         // not a leap year
151         assert_eq!(
152             Time::from_seconds_since_unix_epoch(1492449162),
153             time_from_ymdhms_utc(2017, 4, 17, 17, 12, 42).unwrap()
154         );
155 
156         // leap year, post-feb
157         assert_eq!(
158             Time::from_seconds_since_unix_epoch(1460913162),
159             time_from_ymdhms_utc(2016, 4, 17, 17, 12, 42).unwrap()
160         );
161     }
162 }
163