Skip to content

Commit

Permalink
types: asn1::DateTime: PartialOrd (#402)
Browse files Browse the repository at this point in the history
Signed-off-by: William Woodruff <william@trailofbits.com>
  • Loading branch information
woodruffw authored Jun 15, 2023
1 parent 5d8100b commit bb58b4a
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ fn push_four_digits(dest: &mut WriteBuf, val: u16) -> WriteResult {

/// A structure representing a (UTC timezone) date and time.
/// Wrapped by `UtcTime` and `GeneralizedTime`.
#[derive(Debug, Clone, PartialEq, Hash, Eq)]
#[derive(Debug, Clone, PartialEq, Hash, Eq, PartialOrd)]
pub struct DateTime {
year: u16,
month: u8,
Expand Down Expand Up @@ -1785,6 +1785,7 @@ mod tests {

assert!(s1 == s2);
}

#[test]
fn test_datetime_new() {
assert!(DateTime::new(2038, 13, 1, 12, 0, 0).is_err());
Expand All @@ -1793,6 +1794,25 @@ mod tests {
assert!(DateTime::new(2000, 1, 1, 24, 0, 0).is_err());
}

#[test]
fn test_datetime_partialord() {
let point = DateTime::new(2023, 6, 15, 14, 26, 5).unwrap();

assert!(point < DateTime::new(2023, 6, 15, 14, 26, 6).unwrap());
assert!(point < DateTime::new(2023, 6, 15, 14, 27, 5).unwrap());
assert!(point < DateTime::new(2023, 6, 15, 15, 26, 5).unwrap());
assert!(point < DateTime::new(2023, 6, 16, 14, 26, 5).unwrap());
assert!(point < DateTime::new(2023, 7, 15, 14, 26, 5).unwrap());
assert!(point < DateTime::new(2024, 6, 15, 14, 26, 5).unwrap());

assert!(point > DateTime::new(2023, 6, 15, 14, 26, 4).unwrap());
assert!(point > DateTime::new(2023, 6, 15, 14, 25, 5).unwrap());
assert!(point > DateTime::new(2023, 6, 15, 13, 26, 5).unwrap());
assert!(point > DateTime::new(2023, 6, 14, 14, 26, 5).unwrap());
assert!(point > DateTime::new(2023, 5, 15, 14, 26, 5).unwrap());
assert!(point > DateTime::new(2022, 6, 15, 14, 26, 5).unwrap());
}

#[test]
fn test_utctime_new() {
assert!(UtcTime::new(DateTime::new(1950, 1, 1, 12, 0, 0).unwrap()).is_ok());
Expand Down

0 comments on commit bb58b4a

Please sign in to comment.