alpm_types/date.rs
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
use time::OffsetDateTime;
/// A build date in seconds since the epoch
///
/// This is a type alias for [`i64`].
///
/// # Examples
/// ```
/// use std::num::IntErrorKind;
/// use std::str::FromStr;
///
/// use alpm_types::{BuildDate, Error, FromOffsetDateTime};
/// use time::OffsetDateTime;
///
/// // create BuildDate from OffsetDateTime
/// let datetime = BuildDate::from_offset_datetime(OffsetDateTime::from_unix_timestamp(1).unwrap());
/// assert_eq!(1, datetime);
///
/// // create BuildDate from &str
/// assert_eq!(BuildDate::from_str("1"), Ok(1));
/// assert!(BuildDate::from_str("foo").is_err());
/// ```
pub type BuildDate = i64;
/// A trait for allowing conversion from an [`OffsetDateTime`] to a type.
pub trait FromOffsetDateTime {
fn from_offset_datetime(input: OffsetDateTime) -> Self;
}
impl FromOffsetDateTime for BuildDate {
/// Converts a [`OffsetDateTime`] into a [`BuildDate`].
///
/// Uses the unix timestamp of the [`OffsetDateTime`].
fn from_offset_datetime(input: OffsetDateTime) -> Self {
input.unix_timestamp()
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
#[rstest]
fn datetime_into_builddate() {
let builddate = 1;
let offset_datetime = OffsetDateTime::from_unix_timestamp(1).unwrap();
let datetime: BuildDate = BuildDate::from_offset_datetime(offset_datetime);
assert_eq!(builddate, datetime);
}
}