Skip to content

Commit

Permalink
Add test for lookup_with_dst_transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed May 4, 2023
1 parent 2ba9227 commit 88c23e6
Showing 1 changed file with 124 additions and 1 deletion.
125 changes: 124 additions & 1 deletion src/offset/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,9 @@ impl TzInfo {
#[cfg(test)]
mod tests {
use super::Local;
use crate::offset::local::TzInfo;
use crate::offset::TimeZone;
use crate::{Datelike, Duration, Utc};
use crate::{Datelike, Duration, FixedOffset, LocalResult, NaiveDate, Utc};

#[test]
fn verify_correct_offsets() {
Expand Down Expand Up @@ -360,4 +361,126 @@ mod tests {
timestr
);
}

#[test]
fn test_lookup_with_dst_transitions() {
#[track_caller]
fn compare_lookup(
tz_info: &TzInfo,
y: i32,
m: u32,
d: u32,
h: u32,
n: u32,
s: u32,
result: LocalResult<FixedOffset>,
) {
let dt = NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap();
assert_eq!(tz_info.lookup_with_dst_transitions(&dt), result);
}

// dst transition before std transition
// dst offset > std offset
let std = FixedOffset::east_opt(3 * 60 * 60).unwrap();
let dst = FixedOffset::east_opt(4 * 60 * 60).unwrap();
let tz_info = TzInfo {
std_offset: std,
dst_offset: dst,
std_transition: Some(
NaiveDate::from_ymd_opt(2023, 10, 29).unwrap().and_hms_opt(3, 0, 0).unwrap(),
),
dst_transition: Some(
NaiveDate::from_ymd_opt(2023, 03, 26).unwrap().and_hms_opt(2, 0, 0).unwrap(),
),
};
compare_lookup(&tz_info, 2023, 03, 26, 1, 0, 0, LocalResult::Single(std));
compare_lookup(&tz_info, 2023, 03, 26, 2, 0, 0, LocalResult::Single(std));
compare_lookup(&tz_info, 2023, 03, 26, 2, 30, 0, LocalResult::None);
compare_lookup(&tz_info, 2023, 03, 26, 3, 0, 0, LocalResult::Single(dst));
compare_lookup(&tz_info, 2023, 03, 26, 4, 0, 0, LocalResult::Single(dst));

compare_lookup(&tz_info, 2023, 10, 29, 1, 0, 0, LocalResult::Single(dst));
compare_lookup(&tz_info, 2023, 10, 29, 2, 0, 0, LocalResult::Ambiguous(dst, std));
compare_lookup(&tz_info, 2023, 10, 29, 2, 30, 0, LocalResult::Ambiguous(dst, std));
compare_lookup(&tz_info, 2023, 10, 29, 3, 0, 0, LocalResult::Ambiguous(dst, std));
compare_lookup(&tz_info, 2023, 10, 29, 4, 0, 0, LocalResult::Single(std));

// std transition before dst transition
// dst offset > std offset
let std = FixedOffset::east_opt(-5 * 60 * 60).unwrap();
let dst = FixedOffset::east_opt(-4 * 60 * 60).unwrap();
let tz_info = TzInfo {
std_offset: std,
dst_offset: dst,
std_transition: Some(
NaiveDate::from_ymd_opt(2023, 03, 24).unwrap().and_hms_opt(3, 0, 0).unwrap(),
),
dst_transition: Some(
NaiveDate::from_ymd_opt(2023, 10, 27).unwrap().and_hms_opt(2, 0, 0).unwrap(),
),
};
compare_lookup(&tz_info, 2023, 3, 24, 1, 0, 0, LocalResult::Single(dst));
compare_lookup(&tz_info, 2023, 3, 24, 2, 0, 0, LocalResult::Ambiguous(dst, std));
compare_lookup(&tz_info, 2023, 3, 24, 2, 30, 0, LocalResult::Ambiguous(dst, std));
compare_lookup(&tz_info, 2023, 3, 24, 3, 0, 0, LocalResult::Ambiguous(dst, std));
compare_lookup(&tz_info, 2023, 3, 24, 4, 0, 0, LocalResult::Single(std));

compare_lookup(&tz_info, 2023, 10, 27, 1, 0, 0, LocalResult::Single(std));
compare_lookup(&tz_info, 2023, 10, 27, 2, 0, 0, LocalResult::Single(std));
compare_lookup(&tz_info, 2023, 10, 27, 2, 30, 0, LocalResult::None);
compare_lookup(&tz_info, 2023, 10, 27, 3, 0, 0, LocalResult::Single(dst));
compare_lookup(&tz_info, 2023, 10, 27, 4, 0, 0, LocalResult::Single(dst));

// dst transition before std transition
// dst offset < std offset
let std = FixedOffset::east_opt(3 * 60 * 60).unwrap();
let dst = FixedOffset::east_opt((2 * 60 + 30) * 60).unwrap();
let tz_info = TzInfo {
std_offset: std,
dst_offset: dst,
std_transition: Some(
NaiveDate::from_ymd_opt(2023, 10, 29).unwrap().and_hms_opt(2, 0, 0).unwrap(),
),
dst_transition: Some(
NaiveDate::from_ymd_opt(2023, 03, 26).unwrap().and_hms_opt(2, 30, 0).unwrap(),
),
};
compare_lookup(&tz_info, 2023, 03, 26, 1, 0, 0, LocalResult::Single(std));
compare_lookup(&tz_info, 2023, 03, 26, 2, 0, 0, LocalResult::Ambiguous(std, dst));
compare_lookup(&tz_info, 2023, 03, 26, 2, 15, 0, LocalResult::Ambiguous(std, dst));
compare_lookup(&tz_info, 2023, 03, 26, 2, 30, 0, LocalResult::Ambiguous(std, dst));
compare_lookup(&tz_info, 2023, 03, 26, 3, 0, 0, LocalResult::Single(dst));

compare_lookup(&tz_info, 2023, 10, 29, 1, 0, 0, LocalResult::Single(dst));
compare_lookup(&tz_info, 2023, 10, 29, 2, 0, 0, LocalResult::Single(dst));
compare_lookup(&tz_info, 2023, 10, 29, 2, 15, 0, LocalResult::None);
compare_lookup(&tz_info, 2023, 10, 29, 2, 30, 0, LocalResult::Single(std));
compare_lookup(&tz_info, 2023, 10, 29, 3, 0, 0, LocalResult::Single(std));

// std transition before dst transition
// dst offset < std offset
let std = FixedOffset::east_opt(-(4 * 60 + 30) * 60).unwrap();
let dst = FixedOffset::east_opt(-5 * 60 * 60).unwrap();
let tz_info = TzInfo {
std_offset: std,
dst_offset: dst,
std_transition: Some(
NaiveDate::from_ymd_opt(2023, 03, 24).unwrap().and_hms_opt(2, 0, 0).unwrap(),
),
dst_transition: Some(
NaiveDate::from_ymd_opt(2023, 10, 27).unwrap().and_hms_opt(2, 30, 0).unwrap(),
),
};
compare_lookup(&tz_info, 2023, 3, 24, 1, 0, 0, LocalResult::Single(dst));
compare_lookup(&tz_info, 2023, 3, 24, 2, 0, 0, LocalResult::Single(dst));
compare_lookup(&tz_info, 2023, 3, 24, 2, 15, 0, LocalResult::None);
compare_lookup(&tz_info, 2023, 3, 24, 2, 30, 0, LocalResult::Single(std));
compare_lookup(&tz_info, 2023, 3, 24, 3, 0, 0, LocalResult::Single(std));

compare_lookup(&tz_info, 2023, 10, 27, 1, 0, 0, LocalResult::Single(std));
compare_lookup(&tz_info, 2023, 10, 27, 2, 0, 0, LocalResult::Ambiguous(std, dst));
compare_lookup(&tz_info, 2023, 10, 27, 2, 15, 0, LocalResult::Ambiguous(std, dst));
compare_lookup(&tz_info, 2023, 10, 27, 2, 30, 0, LocalResult::Ambiguous(std, dst));
compare_lookup(&tz_info, 2023, 10, 27, 3, 0, 0, LocalResult::Single(dst));
}
}

0 comments on commit 88c23e6

Please sign in to comment.