#![cfg(feature = "bundled-tzdb")]
use crate::intervals::Span;
use crate::time::{
Date, DateKey, DateRange, Instant, LocalResolution, RuleZone, TimeOfDay, TimeZoneDatabase,
WallEnd, WallRange, Weekday, Weekdays, WeeklyKey, WindowSpec, Zone, ZoneName, ZonePeriod,
ZoneSchedule,
};
fn database() -> TimeZoneDatabase {
TimeZoneDatabase::bundled().expect("the bundled database is present")
}
fn zone(name: &str) -> Zone {
database()
.zone(name)
.unwrap_or_else(|err| panic!("{name} is in the bundled database: {err}"))
}
fn time(hour: u8, minute: u8) -> TimeOfDay {
TimeOfDay::new(hour, minute).unwrap()
}
fn local(zone: &Zone, date: (i16, i8, i8), at: (u8, u8)) -> Instant {
let date = Date::new(date.0, date.1, date.2).unwrap();
match zone.resolve_local(date, time(at.0, at.1)).unwrap() {
LocalResolution::Unique(instant) => instant,
other => panic!(
"{date} {}:{} in {} is not a single instant: {other:?}",
at.0,
at.1,
zone.name()
),
}
}
fn utc(date: (i16, i8, i8), at: (u8, u8)) -> Instant {
local(&Zone::utc(), date, at)
}
fn range(start: (u8, u8), end: (u8, u8)) -> WallRange {
WallRange::new(time(start.0, start.1), WallEnd::Time(time(end.0, end.1))).unwrap()
}
fn named(zone: &Zone) -> RuleZone {
RuleZone::Named(ZoneName::new(zone.name()).unwrap())
}
fn window(zone: RuleZone, keys: Vec<(Weekdays, Vec<WallRange>)>) -> WindowSpec {
WindowSpec::new(
zone,
keys.into_iter()
.map(|(days, hours)| WeeklyKey::new(days, hours).unwrap())
.collect(),
Vec::new(),
)
.unwrap()
}
fn built(
spec: &WindowSpec,
zones: &ZoneSchedule,
from: Instant,
until: Instant,
) -> Vec<(i64, i64)> {
spec.intervals(zones, from, until)
.unwrap()
.spans()
.iter()
.map(|span: &Span| (span.start(), span.end()))
.collect()
}
fn built_named(
spec: &WindowSpec,
at_zone: &Zone,
from: Instant,
until: Instant,
) -> Vec<(i64, i64)> {
built(spec, &ZoneSchedule::fixed(at_zone.clone()), from, until)
}
fn pair(start: Instant, end: Instant) -> (i64, i64) {
(start.minutes(), end.minutes())
}
#[test]
fn a_fall_back_window_starting_inside_the_repeated_hour_splits_in_two() {
let new_york = zone("America/New_York");
let spec = window(
named(&new_york),
vec![(
Weekdays::single(Weekday::Sunday),
vec![range((1, 30), (3, 30))],
)],
);
let spans = built_named(
&spec,
&new_york,
utc((2027, 11, 7), (0, 0)),
utc((2027, 11, 8), (0, 0)),
);
assert_eq!(
spans,
[
pair(utc((2027, 11, 7), (5, 30)), utc((2027, 11, 7), (6, 0))),
pair(utc((2027, 11, 7), (6, 30)), utc((2027, 11, 7), (8, 30))),
]
);
let zones = ZoneSchedule::fixed(new_york.clone());
assert!(!spec.contains(utc((2027, 11, 7), (6, 15)), &zones));
assert_eq!(
new_york.zoned(utc((2027, 11, 7), (6, 15))).time(),
time(1, 15)
);
}
#[test]
fn a_spring_forward_window_spans_the_skipped_hour_without_a_hole() {
let new_york = zone("America/New_York");
let spec = window(
named(&new_york),
vec![(
Weekdays::single(Weekday::Sunday),
vec![range((1, 0), (4, 0))],
)],
);
assert_eq!(
built_named(
&spec,
&new_york,
utc((2027, 3, 14), (0, 0)),
utc((2027, 3, 15), (0, 0)),
),
[pair(utc((2027, 3, 14), (6, 0)), utc((2027, 3, 14), (8, 0)))]
);
let zones = ZoneSchedule::fixed(new_york.clone());
assert!(spec.contains(utc((2027, 3, 14), (6, 30)), &zones));
assert!(spec.contains(utc((2027, 3, 14), (7, 30)), &zones));
assert_eq!(
new_york.zoned(utc((2027, 3, 14), (7, 30))).time(),
time(3, 30)
);
}
#[test]
fn lord_howes_half_hour_fall_back_splits_a_window_by_thirty_minutes() {
let lord_howe = zone("Australia/Lord_Howe");
let spec = window(
named(&lord_howe),
vec![(
Weekdays::single(Weekday::Sunday),
vec![range((1, 45), (3, 0))],
)],
);
let spans = built_named(
&spec,
&lord_howe,
utc((2027, 4, 3), (0, 0)),
utc((2027, 4, 4), (0, 0)),
);
assert_eq!(
spans,
[
pair(utc((2027, 4, 3), (14, 45)), utc((2027, 4, 3), (15, 0))),
pair(utc((2027, 4, 3), (15, 15)), utc((2027, 4, 3), (16, 30))),
]
);
}
#[test]
fn a_skipped_local_midnight_starts_the_date_at_the_transition() {
let santiago = zone("America/Santiago");
assert_eq!(
santiago
.resolve_local(Date::new(2027, 9, 5).unwrap(), TimeOfDay::MIDNIGHT)
.unwrap(),
LocalResolution::Skipped
);
let spec = window(
named(&santiago),
vec![(
Weekdays::single(Weekday::Sunday),
vec![range((0, 0), (2, 0))],
)],
);
assert_eq!(
built_named(
&spec,
&santiago,
utc((2027, 9, 5), (0, 0)),
utc((2027, 9, 6), (0, 0)),
),
[pair(utc((2027, 9, 5), (4, 0)), utc((2027, 9, 5), (5, 0)))]
);
}
#[test]
fn a_leap_day_override_covers_the_date_inside_its_range() {
let date = DateKey::new(
DateRange::new(
Date::new(2028, 2, 28).unwrap(),
Date::new(2028, 3, 1).unwrap(),
)
.unwrap(),
vec![range((10, 0), (12, 0))],
)
.unwrap();
let spec = WindowSpec::new(named(&Zone::utc()), Vec::new(), vec![date]).unwrap();
assert_eq!(
built_named(
&spec,
&Zone::utc(),
utc((2028, 2, 27), (0, 0)),
utc((2028, 3, 2), (0, 0)),
),
[
pair(utc((2028, 2, 28), (10, 0)), utc((2028, 2, 28), (12, 0))),
pair(utc((2028, 2, 29), (10, 0)), utc((2028, 2, 29), (12, 0))),
pair(utc((2028, 3, 1), (10, 0)), utc((2028, 3, 1), (12, 0))),
]
);
assert!(spec.contains(
utc((2028, 2, 29), (11, 0)),
&ZoneSchedule::fixed(Zone::utc()),
));
}
#[test]
fn a_mid_day_zone_period_switch_reads_the_window_in_each_zones_own_clock() {
let tokyo = zone("Asia/Tokyo");
let los_angeles = zone("America/Los_Angeles");
let schedule = ZoneSchedule::new(
tokyo.clone(),
vec![
ZonePeriod::new(
los_angeles.clone(),
utc((2027, 3, 9), (16, 0)),
utc((2027, 3, 10), (0, 0)),
)
.unwrap(),
],
)
.unwrap();
let spec = window(
RuleZone::Participant,
vec![(
Weekdays::range(Weekday::Monday, Weekday::Sunday),
vec![range((9, 0), (10, 0))],
)],
);
assert_eq!(
built(
&spec,
&schedule,
utc((2027, 3, 9), (0, 0)),
utc((2027, 3, 10), (2, 0)),
),
[
pair(utc((2027, 3, 9), (0, 0)), utc((2027, 3, 9), (1, 0))),
pair(utc((2027, 3, 9), (17, 0)), utc((2027, 3, 9), (18, 0))),
pair(utc((2027, 3, 10), (0, 0)), utc((2027, 3, 10), (1, 0))),
]
);
}
#[test]
fn a_period_crossing_the_date_line_repeats_a_civil_date() {
let auckland = zone("Pacific/Auckland");
let honolulu = zone("Pacific/Honolulu");
let schedule = ZoneSchedule::new(
auckland.clone(),
vec![
ZonePeriod::new(
honolulu.clone(),
local(&auckland, (2027, 3, 9), (23, 0)),
local(&auckland, (2027, 3, 11), (23, 0)),
)
.unwrap(),
],
)
.unwrap();
let spec = window(
RuleZone::Participant,
vec![(
Weekdays::list([Weekday::Monday, Weekday::Tuesday, Weekday::Wednesday]),
vec![range((9, 0), (10, 0))],
)],
);
assert_eq!(
built(
&spec,
&schedule,
utc((2027, 3, 8), (0, 0)),
utc((2027, 3, 11), (12, 0)),
),
[
pair(utc((2027, 3, 8), (20, 0)), utc((2027, 3, 8), (21, 0))),
pair(utc((2027, 3, 9), (19, 0)), utc((2027, 3, 9), (20, 0))),
pair(utc((2027, 3, 10), (19, 0)), utc((2027, 3, 10), (20, 0))),
]
);
}
#[test]
fn a_period_crossing_the_date_line_skips_a_civil_date() {
let west = zone("Etc/GMT+12");
let kiritimati = zone("Pacific/Kiritimati");
let switch = local(&west, (2027, 3, 9), (23, 0));
let schedule = ZoneSchedule::new(
west.clone(),
vec![
ZonePeriod::new(
kiritimati.clone(),
switch,
local(&west, (2027, 3, 12), (0, 0)),
)
.unwrap(),
],
)
.unwrap();
let spec = window(
RuleZone::Participant,
vec![(
Weekdays::list([
Weekday::Tuesday,
Weekday::Wednesday,
Weekday::Thursday,
Weekday::Friday,
]),
vec![range((9, 0), (10, 0))],
)],
);
assert_eq!(
built(
&spec,
&schedule,
utc((2027, 3, 9), (12, 0)),
utc((2027, 3, 11), (12, 0)),
),
[
pair(utc((2027, 3, 9), (21, 0)), utc((2027, 3, 9), (22, 0))),
pair(utc((2027, 3, 10), (19, 0)), utc((2027, 3, 10), (20, 0))),
]
);
let zones = &schedule;
let mut minute = switch.minutes();
while minute < utc((2027, 3, 10), (19, 0)).minutes() {
let instant = Instant::from_minutes(minute).unwrap();
assert_ne!(
zones.zone_at(instant).zoned(instant).date(),
Date::new(2027, 3, 10).unwrap()
);
minute += 30;
}
}
#[test]
fn a_window_range_ending_at_24_00_stops_at_midnight() {
let spec = window(
named(&Zone::utc()),
vec![(
Weekdays::single(Weekday::Friday),
vec![WallRange::new(time(20, 0), WallEnd::Midnight).unwrap()],
)],
);
assert_eq!(
built_named(
&spec,
&Zone::utc(),
utc((2027, 3, 12), (0, 0)),
utc((2027, 3, 13), (0, 0)),
),
[pair(
utc((2027, 3, 12), (20, 0)),
utc((2027, 3, 13), (0, 0))
)]
);
let zones = ZoneSchedule::fixed(Zone::utc());
assert!(!spec.contains(utc((2027, 3, 13), (0, 0)), &zones));
}
#[test]
fn a_zone_period_starting_on_a_dst_day_reads_each_zone_at_its_own_offset() {
let tokyo = zone("Asia/Tokyo");
let new_york = zone("America/New_York");
let schedule = ZoneSchedule::new(
tokyo,
vec![
ZonePeriod::new(
new_york.clone(),
utc((2027, 3, 14), (5, 0)),
utc((2027, 3, 15), (5, 0)),
)
.unwrap(),
],
)
.unwrap();
let spec = window(
RuleZone::Participant,
vec![(
Weekdays::range(Weekday::Monday, Weekday::Sunday),
vec![range((1, 0), (4, 0))],
)],
);
assert_eq!(
built(
&spec,
&schedule,
utc((2027, 3, 13), (0, 0)),
utc((2027, 3, 15), (0, 0)),
),
[
pair(utc((2027, 3, 13), (16, 0)), utc((2027, 3, 13), (19, 0))),
pair(utc((2027, 3, 14), (6, 0)), utc((2027, 3, 14), (8, 0))),
]
);
}