monorepo/timezone-testing/src/lib.rs

53 lines
1.5 KiB
Rust
Raw Normal View History

#[cfg(test)]
mod tests {
use chrono::{DateTime, FixedOffset, NaiveDate, TimeZone};
use chrono_tz::{
America::{New_York, Phoenix},
Tz,
US::Mountain,
};
#[test]
fn it_saves_with_offset() {
let date = Phoenix.with_ymd_and_hms(2023, 10, 13, 23, 0, 0).unwrap();
assert_eq!(date.to_rfc3339(), "2023-10-13T23:00:00-07:00");
assert_eq!(date.date_naive().to_string(), "2023-10-13");
assert_eq!(
date.with_timezone(&New_York).to_rfc3339(),
"2023-10-14T02:00:00-04:00"
);
assert_eq!(
date.with_timezone(&New_York).date_naive().to_string(),
"2023-10-14"
);
}
#[test]
fn it_can_parse_with_offset() {
let reference = "2023-10-13T23:00:00-08:00";
let date = DateTime::parse_from_rfc3339(reference).unwrap();
assert_eq!(
date,
FixedOffset::west_opt(8 * 60 * 60)
.unwrap()
.with_ymd_and_hms(2023, 10, 13, 23, 0, 0)
.unwrap()
);
assert_eq!(
date.with_timezone(&New_York),
FixedOffset::west_opt(4 * 60 * 60)
.unwrap()
.with_ymd_and_hms(2023, 10, 14, 03, 0, 0)
.unwrap()
);
assert_eq!(
date.date_naive(),
NaiveDate::from_ymd_opt(2023, 10, 13).unwrap()
);
assert_eq!(
date.with_timezone(&New_York).date_naive(),
NaiveDate::from_ymd_opt(2023, 10, 14).unwrap()
);
}
}