webpki/
calendar.rs

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
15use 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!(), // `read_two_digits` already bounds-checked it.
47    };
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    // We don't support dates before January 1, 1970 because that is the
62    // Unix epoch. It is likely that other software won't deal well with
63    // certificates that have dates before the epoch.
64    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)    // leap years are every 4 years,
75        - ((year - 1) / 100)  // except years divisible by 100,
76        + ((year - 1) / 400) // except years divisible by 400.
77}
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!(), // `read_two_digits` already bounds-checked it.
85    }
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        // leap cases
123        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        // year boundary
134        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        // not a leap year
144        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        // leap year, post-feb
150        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}