From 85e2494c3bbbd12b8a582728a88242b334f1873f Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Tue, 26 Dec 2023 13:33:18 -0500 Subject: [PATCH] Add a test application that demonstrates chrono and timezone processing --- Cargo.lock | 8 ++++++ Cargo.toml | 1 + timezone-testing/Cargo.toml | 10 +++++++ timezone-testing/src/lib.rs | 52 +++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 timezone-testing/Cargo.toml create mode 100644 timezone-testing/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index c2c4b54..3ea224d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 73f671e..af6549d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ members = [ "result-extended", "screenplay", "sgf", + "timezone-testing", "tree", "visions/server", ] diff --git a/timezone-testing/Cargo.toml b/timezone-testing/Cargo.toml new file mode 100644 index 0000000..35f78c9 --- /dev/null +++ b/timezone-testing/Cargo.toml @@ -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" diff --git a/timezone-testing/src/lib.rs b/timezone-testing/src/lib.rs new file mode 100644 index 0000000..8ee8dc1 --- /dev/null +++ b/timezone-testing/src/lib.rs @@ -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() + ); + } +}