webpki/src/calendar.rs

163 lines
5.5 KiB
Rust
Raw Permalink Normal View History

// 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.
2019-03-29 01:22:01 +00:00
use super::{time::Time, Error};
2019-03-29 01:22:01 +00:00
pub fn time_from_ymdhms_utc(
2020-12-29 20:42:10 +00:00
year: u64,
month: u64,
day_of_month: u64,
hours: u64,
minutes: u64,
seconds: u64,
2019-03-29 01:22:01 +00:00
) -> 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 {
2019-03-29 01:22:01 +00:00
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,
2019-03-29 01:22:01 +00:00
_ => unreachable!(), // `read_two_digits` already bounds-checked it.
};
2019-03-29 01:22:01 +00:00
let days_before =
days_before_year_since_unix_epoch + days_before_month_in_year + day_of_month - 1;
2019-03-29 01:22:01 +00:00
let seconds_since_unix_epoch =
(days_before * 24 * 60 * 60) + (hours * 60 * 60) + (minutes * 60) + seconds;
2019-03-29 01:22:01 +00:00
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 {
Revert main branch crate contents to the 0.22.0 release contents. Reset the crate contents (sources, tests, etc.) to what they were at that commit, while retaining the newer CI configuration. The changes since the 0.22.0 release were primarily intended to accomplish two goals: * Fix and improve the GitHub Actions configuration. * Prepare a 0.21.5 release that was backward compatible with 0.21.4 but which also contained the improvements that were in 0.22.0. 0.21.5 was never released and will not be released. Therefore all of the noise to facilitate the 0.21.5 release can just be deleted, as long as we leave the CI changes that are necessary for GitHub Actions to work correctly now. The exact commands I used were: ``` git checkout \ 6c334a2cf5853fb0aa93b5eb0318c031fc2f6f98 \ -- \ Cargo.toml \ LICENSE \ README.md \ src \ tests \ third-party git rm src/trust_anchor_util.rs ``` Commit 6c334a2cf5853fb0aa93b5eb0318c031fc2f6f98 was the commit from which 0.22.0 was released. It is confusing because the commit immediately prior, 0b7cbf2d327d7665d9d06072bf46b2e7ca05f065, has commit message "0.22.0". It appears that I merged the "0.22.0" commit, expecting to `cargo publish` from that commit, but then `cargo publish` failed. Then I added 6c334a2cf5853fb0aa93b5eb0318c031fc2f6f98 to fix `cargo publish` and did the `cargo publish` from that commit. That's why I added the `package` CI step at that time, to prevent this confusing situation from happening again. `trust_anchor_utils.rs` was not in 0.22.0; the `git checkout` didn't delete it, so I had to do it separately. I left the tests added subsequent to 0.22.0 in `tests/` (e.g. `name_tests.rs`) since those tests pass with the 0.22.0 sources too. Unfortunately, this requires disabling a bunch of Clippy lints, to avoid modifying the contents from 0.22.0. (I know it is confusing. It took me a while to figure it out myself today.)
2023-08-30 01:13:07 +00:00
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,
2019-03-29 01:22:01 +00:00
+ ((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),
2019-03-29 01:22:01 +00:00
_ => 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
}
}
#[allow(clippy::unreadable_literal)] // TODO: Make this clear.
const DAYS_BEFORE_UNIX_EPOCH_AD: u64 = 719162;
#[cfg(test)]
mod tests {
#[test]
fn test_days_before_unix_epoch() {
2019-03-29 01:22:01 +00:00
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);
}
#[allow(clippy::unreadable_literal)] // TODO: Make this clear.
#[test]
fn test_time_from_ymdhms_utc() {
use super::{time_from_ymdhms_utc, Time};
// year boundary
2019-03-29 01:22:01 +00:00
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
2019-03-29 01:22:01 +00:00
assert_eq!(
Time::from_seconds_since_unix_epoch(1492449162),
time_from_ymdhms_utc(2017, 4, 17, 17, 12, 42).unwrap()
);
// leap year, post-feb
2019-03-29 01:22:01 +00:00
assert_eq!(
Time::from_seconds_since_unix_epoch(1460913162),
time_from_ymdhms_utc(2016, 4, 17, 17, 12, 42).unwrap()
);
}
}