1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2015-2016 Brian Smith.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use super::{time::Time, Error};

pub fn time_from_ymdhms_utc(
    year: u64, month: u64, day_of_month: u64, hours: u64, minutes: u64, seconds: u64,
) -> Result<Time, Error> {
    let days_before_year_since_unix_epoch = days_before_year_since_unix_epoch(year)?;

    const JAN: u64 = 31;
    let feb = days_in_feb(year);
    const MAR: u64 = 31;
    const APR: u64 = 30;
    const MAY: u64 = 31;
    const JUN: u64 = 30;
    const JUL: u64 = 31;
    const AUG: u64 = 31;
    const SEP: u64 = 30;
    const OCT: u64 = 31;
    const NOV: u64 = 30;
    let days_before_month_in_year = match month {
        1 => 0,
        2 => JAN,
        3 => JAN + feb,
        4 => JAN + feb + MAR,
        5 => JAN + feb + MAR + APR,
        6 => JAN + feb + MAR + APR + MAY,
        7 => JAN + feb + MAR + APR + MAY + JUN,
        8 => JAN + feb + MAR + APR + MAY + JUN + JUL,
        9 => JAN + feb + MAR + APR + MAY + JUN + JUL + AUG,
        10 => JAN + feb + MAR + APR + MAY + JUN + JUL + AUG + SEP,
        11 => JAN + feb + MAR + APR + MAY + JUN + JUL + AUG + SEP + OCT,
        12 => JAN + feb + MAR + APR + MAY + JUN + JUL + AUG + SEP + OCT + NOV,
        _ => unreachable!(), // `read_two_digits` already bounds-checked it.
    };

    let days_before =
        days_before_year_since_unix_epoch + days_before_month_in_year + day_of_month - 1;

    let seconds_since_unix_epoch =
        (days_before * 24 * 60 * 60) + (hours * 60 * 60) + (minutes * 60) + seconds;

    Ok(Time::from_seconds_since_unix_epoch(
        seconds_since_unix_epoch,
    ))
}

fn days_before_year_since_unix_epoch(year: u64) -> Result<u64, Error> {
    // We don't support dates before January 1, 1970 because that is the
    // Unix epoch. It is likely that other software won't deal well with
    // certificates that have dates before the epoch.
    if year < 1970 {
        return Err(Error::BadDERTime);
    }
    let days_before_year_ad = days_before_year_ad(year);
    debug_assert!(days_before_year_ad >= DAYS_BEFORE_UNIX_EPOCH_AD);
    Ok(days_before_year_ad - DAYS_BEFORE_UNIX_EPOCH_AD)
}

fn days_before_year_ad(year: u64) -> u64 {
    ((year - 1) * 365)
        + ((year - 1) / 4)    // leap years are every 4 years,
        - ((year - 1) / 100)  // except years divisible by 100,
        + ((year - 1) / 400) // except years divisible by 400.
}

pub fn days_in_month(year: u64, month: u64) -> u64 {
    match month {
        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
        4 | 6 | 9 | 11 => 30,
        2 => days_in_feb(year),
        _ => unreachable!(), // `read_two_digits` already bounds-checked it.
    }
}

fn days_in_feb(year: u64) -> u64 {
    if (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) {
        29
    } else {
        28
    }
}

const DAYS_BEFORE_UNIX_EPOCH_AD: u64 = 719162;

#[cfg(test)]
mod tests {
    #[test]
    fn test_days_before_unix_epoch() {
        use super::{days_before_year_ad, DAYS_BEFORE_UNIX_EPOCH_AD};
        assert_eq!(DAYS_BEFORE_UNIX_EPOCH_AD, days_before_year_ad(1970));
    }

    #[test]
    fn test_days_in_month() {
        use super::days_in_month;
        assert_eq!(days_in_month(2017, 1), 31);
        assert_eq!(days_in_month(2017, 2), 28);
        assert_eq!(days_in_month(2017, 3), 31);
        assert_eq!(days_in_month(2017, 4), 30);
        assert_eq!(days_in_month(2017, 5), 31);
        assert_eq!(days_in_month(2017, 6), 30);
        assert_eq!(days_in_month(2017, 7), 31);
        assert_eq!(days_in_month(2017, 8), 31);
        assert_eq!(days_in_month(2017, 9), 30);
        assert_eq!(days_in_month(2017, 10), 31);
        assert_eq!(days_in_month(2017, 11), 30);
        assert_eq!(days_in_month(2017, 12), 31);

        // leap cases
        assert_eq!(days_in_month(2000, 2), 29);
        assert_eq!(days_in_month(2004, 2), 29);
        assert_eq!(days_in_month(2016, 2), 29);
        assert_eq!(days_in_month(2100, 2), 28);
    }

    #[test]
    fn test_time_from_ymdhms_utc() {
        use super::{time_from_ymdhms_utc, Time};

        // year boundary
        assert_eq!(
            Time::from_seconds_since_unix_epoch(1483228799),
            time_from_ymdhms_utc(2016, 12, 31, 23, 59, 59).unwrap()
        );
        assert_eq!(
            Time::from_seconds_since_unix_epoch(1483228800),
            time_from_ymdhms_utc(2017, 1, 1, 0, 0, 0).unwrap()
        );

        // not a leap year
        assert_eq!(
            Time::from_seconds_since_unix_epoch(1492449162),
            time_from_ymdhms_utc(2017, 4, 17, 17, 12, 42).unwrap()
        );

        // leap year, post-feb
        assert_eq!(
            Time::from_seconds_since_unix_epoch(1460913162),
            time_from_ymdhms_utc(2016, 4, 17, 17, 12, 42).unwrap()
        );
    }
}