Add a bunch of tests

This commit is contained in:
Nathan Fisher 2024-12-17 12:19:45 -05:00
parent 4050533558
commit 3c965faf86
2 changed files with 65 additions and 4 deletions

View File

@ -89,7 +89,7 @@ impl DateTime {
/// Gets the Unix timestamp corresponding to this `DateTime`
pub fn timestamp(&self) -> i64 {
let mut seconds = self.timestamp_naive();
seconds -= self.zone.as_seconds();
seconds += self.zone.as_seconds();
seconds
}
@ -188,7 +188,7 @@ impl DateTime {
}
/// Converts this datetime to a different timezone
pub fn convert_timezone(&mut self, zone:TimeZone) {
pub fn convert_timezone(&mut self, zone: TimeZone) {
let ts = self.timestamp() + zone.as_seconds();
*self = Self::from_timestamp(ts);
self.zone = zone;
@ -279,6 +279,7 @@ impl PartialOrd for DateTime {
#[cfg(test)]
mod tests {
use super::*;
use crate::zone::Sign;
#[test]
fn weekday() {
@ -390,7 +391,7 @@ mod tests {
let mut dt = DateTime::from_timestamp(1706571482);
assert_eq!(dt.to_string(), "2024-01-29T23:38:02Z");
dt.zone = TimeZone::Offset {
sign: crate::zone::Sign::Positive,
sign: Sign::Positive,
hours: 4,
minutes: 15,
};
@ -402,10 +403,59 @@ mod tests {
let mut dt = DateTime::from_timestamp(1706571482);
assert_eq!(dt.display(), "Mon Jan 29 23:38:02 UTC 2024");
dt.zone = TimeZone::Offset {
sign: crate::zone::Sign::Positive,
sign: Sign::Positive,
hours: 4,
minutes: 15,
};
assert_eq!(dt.display(), "Mon Jan 29 23:38:02 +04:15 2024");
}
#[test]
fn convert_zone() {
let mut dt = DateTime {
year: Year::new(2024),
month: Month::Janurary,
day: 29,
hour: 23,
minute: 38,
second: 02,
zone: TimeZone::Utc,
};
let tz = TimeZone::Offset {
sign: Sign::Negative,
hours: 5,
minutes: 0,
};
dt.convert_timezone(tz);
assert_eq!(
dt,
DateTime {
year: Year::new(2024),
month: Month::Janurary,
day: 29,
hour: 18,
minute: 38,
second: 02,
zone: tz,
}
);
}
#[test]
fn from_timestamp_with_zone() {
let tz = TimeZone::Offset { sign: Sign::Negative, hours: 5, minutes: 0 };
let dt = DateTime::from_timestamp_with_zone(1706571482, tz);
assert_eq!(
dt,
DateTime {
year: Year::new(2024),
month: Month::Janurary,
day: 29,
hour: 18,
minute: 38,
second: 02,
zone: tz,
}
);
}
}

View File

@ -168,3 +168,14 @@ impl fmt::Display for TZError {
}
impl error::Error for TZError {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn as_seconds() {
let tz = TimeZone::Offset { sign: Sign::Negative, hours: 5, minutes: 0 };
assert_eq!(tz.as_seconds(), -18000);
}
}