Merge pull request #142 from sorairolake/impl-more-traits

Implement `PartialEq`, `Eq`, `PartialOrd`, `Ord`, `Hash` and `Display` for `DateTime`
This commit is contained in:
Chris Hennick 2024-05-23 03:37:56 +00:00 committed by GitHub
commit 90fcad6e9a
Signed by: DevComp
GPG key ID: B5690EEEBB952194

View file

@ -1,5 +1,6 @@
//! Types that specify what is contained in a ZIP. //! Types that specify what is contained in a ZIP.
use path::{Component, Path, PathBuf}; use path::{Component, Path, PathBuf};
use std::fmt;
use std::path; use std::path;
use std::sync::{Arc, OnceLock}; use std::sync::{Arc, OnceLock};
@ -67,7 +68,7 @@ impl From<System> for u8 {
/// ///
/// Modern zip files store more precise timestamps, which are ignored by [`crate::read::ZipArchive`], /// Modern zip files store more precise timestamps, which are ignored by [`crate::read::ZipArchive`],
/// so keep in mind that these timestamps are unreliable. [We're working on this](https://github.com/zip-rs/zip/issues/156#issuecomment-652981904). /// so keep in mind that these timestamps are unreliable. [We're working on this](https://github.com/zip-rs/zip/issues/156#issuecomment-652981904).
#[derive(Debug, Clone, Copy)] #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct DateTime { pub struct DateTime {
year: u16, year: u16,
month: u8, month: u8,
@ -135,6 +136,17 @@ impl Default for DateTime {
} }
} }
impl fmt::Display for DateTime {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{:04}-{:02}-{:02} {:02}:{:02}:{:02}",
self.year, self.month, self.day, self.hour, self.minute, self.second
)
}
}
impl DateTime { impl DateTime {
/// Converts an msdos (u16, u16) pair to a DateTime object /// Converts an msdos (u16, u16) pair to a DateTime object
pub const fn from_msdos(datepart: u16, timepart: u16) -> DateTime { pub const fn from_msdos(datepart: u16, timepart: u16) -> DateTime {
@ -606,6 +618,70 @@ mod test {
assert_eq!(dt.datepart(), 0b1111111_1100_11111); assert_eq!(dt.datepart(), 0b1111111_1100_11111);
} }
#[test]
fn datetime_equality() {
use super::DateTime;
let dt = DateTime::from_date_and_time(2018, 11, 17, 10, 38, 30).unwrap();
assert_eq!(
dt,
DateTime::from_date_and_time(2018, 11, 17, 10, 38, 30).unwrap()
);
assert_ne!(dt, DateTime::default());
}
#[test]
fn datetime_order() {
use std::cmp::Ordering;
use super::DateTime;
let dt = DateTime::from_date_and_time(2018, 11, 17, 10, 38, 30).unwrap();
assert_eq!(
dt.cmp(&DateTime::from_date_and_time(2018, 11, 17, 10, 38, 30).unwrap()),
Ordering::Equal
);
// year
assert!(dt < DateTime::from_date_and_time(2019, 11, 17, 10, 38, 30).unwrap());
assert!(dt > DateTime::from_date_and_time(2017, 11, 17, 10, 38, 30).unwrap());
// month
assert!(dt < DateTime::from_date_and_time(2018, 12, 17, 10, 38, 30).unwrap());
assert!(dt > DateTime::from_date_and_time(2018, 10, 17, 10, 38, 30).unwrap());
// day
assert!(dt < DateTime::from_date_and_time(2018, 11, 18, 10, 38, 30).unwrap());
assert!(dt > DateTime::from_date_and_time(2018, 11, 16, 10, 38, 30).unwrap());
// hour
assert!(dt < DateTime::from_date_and_time(2018, 11, 17, 11, 38, 30).unwrap());
assert!(dt > DateTime::from_date_and_time(2018, 11, 17, 9, 38, 30).unwrap());
// minute
assert!(dt < DateTime::from_date_and_time(2018, 11, 17, 10, 39, 30).unwrap());
assert!(dt > DateTime::from_date_and_time(2018, 11, 17, 10, 37, 30).unwrap());
// second
assert!(dt < DateTime::from_date_and_time(2018, 11, 17, 10, 38, 31).unwrap());
assert!(dt > DateTime::from_date_and_time(2018, 11, 17, 10, 38, 29).unwrap());
}
#[test]
fn datetime_display() {
use super::DateTime;
assert_eq!(format!("{}", DateTime::default()), "1980-01-01 00:00:00");
assert_eq!(
format!(
"{}",
DateTime::from_date_and_time(2018, 11, 17, 10, 38, 30).unwrap()
),
"2018-11-17 10:38:30"
);
assert_eq!(
format!(
"{}",
DateTime::from_date_and_time(2107, 12, 31, 23, 59, 59).unwrap()
),
"2107-12-31 23:59:59"
);
}
#[test] #[test]
fn datetime_bounds() { fn datetime_bounds() {
use super::DateTime; use super::DateTime;