Skip to content

Commit

Permalink
Remove unused methods from Date
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Apr 14, 2024
1 parent 62f0e66 commit a37de8e
Showing 1 changed file with 9 additions and 34 deletions.
43 changes: 9 additions & 34 deletions src/private/date.rs
Expand Up @@ -8,8 +8,6 @@ pub struct Error(#[from] ErrorKind);
enum ErrorKind {
#[error("invalid format : {0}")]
InvalidFormat(String),
#[error("timestamp is out of range : {0}")]
TimestampOutOfRange(i64),
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
Expand All @@ -19,12 +17,6 @@ impl Date {
pub(crate) fn from_unix_timestamp_obj(unix_timestamp: UnixTimestamp) -> Self {
Self(unix_timestamp.to_date())
}

pub fn from_unix_timestamp(unix_timestamp: i64) -> Result<Self, Error> {
Ok(UnixTimestamp::try_from(unix_timestamp)
.map(|unix_timestamp| Self(unix_timestamp.to_date()))
.map_err(|_| ErrorKind::TimestampOutOfRange(unix_timestamp))?)
}
}

impl std::convert::TryFrom<&str> for Date {
Expand Down Expand Up @@ -66,34 +58,17 @@ mod tests {

#[test]
fn test() -> anyhow::Result<()> {
let min_unix_timestamp =
i64::from(UnixTimestamp::from_rfc3339("0000-01-01T00:00:00+00:00")?);
assert!(Date::from_unix_timestamp(min_unix_timestamp - 1).is_err());
assert!(Date::from_unix_timestamp(min_unix_timestamp).is_ok());

let unix_timestamp = i64::from(UnixTimestamp::from_rfc3339("2020-01-02T03:04:05+00:00")?);
let date = Date::from_unix_timestamp(unix_timestamp);
assert_eq!(date?.to_string(), "20200102");
let min_unix_timestamp = UnixTimestamp::from_rfc3339("0000-01-01T00:00:00+00:00")?;
let date = Date::from_unix_timestamp_obj(min_unix_timestamp);
assert_eq!(date.to_string(), "00000101");

let max_unix_timestamp =
i64::from(UnixTimestamp::from_rfc3339("9999-12-31T23:59:59+00:00")?);
assert!(Date::from_unix_timestamp(max_unix_timestamp).is_ok());
assert!(Date::from_unix_timestamp(max_unix_timestamp + 1).is_err());
let unix_timestamp = UnixTimestamp::from_rfc3339("2020-01-02T03:04:05+00:00")?;
let date = Date::from_unix_timestamp_obj(unix_timestamp);
assert_eq!(date.to_string(), "20200102");

assert!(Date::from_unix_timestamp(-62_167_219_201).is_err());
assert_eq!(
Date::from_unix_timestamp(-62_167_219_200)?.to_string(),
"00000101"
);
assert_eq!(Date::from_unix_timestamp(-1)?.to_string(), "19691231");
assert_eq!(Date::from_unix_timestamp(0)?.to_string(), "19700101");
assert_eq!(Date::from_unix_timestamp(86399)?.to_string(), "19700101");
assert_eq!(Date::from_unix_timestamp(86400)?.to_string(), "19700102");
assert_eq!(
Date::from_unix_timestamp(253_402_300_799)?.to_string(),
"99991231"
);
assert!(Date::from_unix_timestamp(253_402_300_800).is_err());
let max_unix_timestamp = UnixTimestamp::from_rfc3339("9999-12-31T23:59:59+00:00")?;
let date = Date::from_unix_timestamp_obj(max_unix_timestamp);
assert_eq!(date.to_string(), "99991231");
Ok(())
}

Expand Down

0 comments on commit a37de8e

Please sign in to comment.