Add a test application that demonstrates chrono and timezone processing

pull/129/head
Savanni D'Gerinel 2023-12-26 13:33:18 -05:00
parent af8f9b0244
commit 85e2494c3b
4 changed files with 71 additions and 0 deletions

8
Cargo.lock generated
View File

@ -4202,6 +4202,14 @@ dependencies = [
"time-core",
]
[[package]]
name = "timezone-testing"
version = "0.1.0"
dependencies = [
"chrono",
"chrono-tz",
]
[[package]]
name = "tinystr"
version = "0.7.5"

View File

@ -25,6 +25,7 @@ members = [
"result-extended",
"screenplay",
"sgf",
"timezone-testing",
"tree",
"visions/server",
]

View File

@ -0,0 +1,10 @@
[package]
name = "timezone-testing"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chrono = "0.4.31"
chrono-tz = "0.8.4"

View File

@ -0,0 +1,52 @@
#[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()
);
}
}