//! Tests against the real downloaded data (`scripts/fetch-deps.sh`). use std::path::PathBuf; use std::sync::OnceLock; use geo_tz::{AmbiguityPolicy, Atlas, GeoError, TransitionKind, ZoneSource}; use jiff::civil::date; fn atlas() -> &'static Atlas { static ATLAS: OnceLock = OnceLock::new(); ATLAS.get_or_init(|| { let dir = std::env::var_os("ASTRO_DATA_DIR") .map(PathBuf::from) .unwrap_or_else(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../data")); Atlas::load(&dir).expect("data directory (run scripts/fetch-deps.sh)") }) } #[test] fn country_lookup() { let a = atlas(); assert_eq!(a.places.country("HU").unwrap().name, "Hungary"); assert_eq!(a.places.country("hun").unwrap().iso2, "HU"); assert_eq!(a.places.country("hungary").unwrap().iso2, "HU"); assert!(matches!(a.places.country("XX"), Err(GeoError::UnknownCountry(_)))); } #[test] fn place_search_handles_accents_villages_and_foreign_names() { let a = atlas(); let hu = a.places.country("HU").unwrap(); let szeged = &a.places.search(hu, "Szeged", None, 5)[0]; assert_eq!(szeged.place.geonames_id, 715429); assert_eq!(szeged.place.timezone, "Europe/Budapest"); let hmv = a.places.search(hu, "hodmezovasarhely", None, 5); assert_eq!(hmv[0].place.name, "Hódmezővásárhely"); // a tiny village (population well below 500) from the full HU list let village = a.places.search(hu, "Iborfia", None, 5); assert!(!village.is_empty(), "small villages must be found"); // Hungarian name of a Romanian city let ro = a.places.country("RO").unwrap(); let cluj = &a.places.search(ro, "Kolozsvár", None, 5)[0]; assert_eq!(cluj.place.name, "Cluj-Napoca"); assert_eq!(cluj.place.timezone, "Europe/Bucharest"); // prefix fallback assert!(!a.places.search(hu, "Hódmező", None, 5).is_empty()); } #[test] fn zone_choice() { let a = atlas(); let hu = a.places.country("HU").unwrap(); let z = a.choose_zone(hu, None, None, (47.5, 19.0)).unwrap(); assert_eq!((z.zone.as_str(), z.source), ("Europe/Budapest", ZoneSource::Country)); // multi-zone country resolved from coordinates (Denver) let us = a.places.country("US").unwrap(); let z = a.choose_zone(us, None, None, (39.74, -104.99)).unwrap(); assert_eq!((z.zone.as_str(), z.source), ("America/Denver", ZoneSource::NearestPlace)); assert!(a.choose_zone(hu, Some("Mars/Olympus"), None, (0.0, 0.0)).is_err()); } #[test] fn hungarian_dst_history() { let tz = &atlas().zones; // 1985 summer: CEST let t = tz.localize("Europe/Budapest", date(1985, 7, 14).at(14, 30, 0, 0), AmbiguityPolicy::Reject).unwrap(); assert_eq!((t.utc_offset.as_str(), t.dst, t.abbreviation.as_str()), ("+02:00", true, "CEST")); assert_eq!(t.utc, "1985-07-14T12:30:00Z"); // 1970 summer: Hungary had no DST between 1958 and 1979 let t = tz.localize("Europe/Budapest", date(1970, 7, 1).at(12, 0, 0, 0), AmbiguityPolicy::Reject).unwrap(); assert_eq!((t.utc_offset.as_str(), t.dst), ("+01:00", false)); // 1956 summer: DST was used let t = tz.localize("Europe/Budapest", date(1956, 7, 1).at(12, 0, 0, 0), AmbiguityPolicy::Reject).unwrap(); assert_eq!((t.utc_offset.as_str(), t.dst), ("+02:00", true)); // before 1890: local mean time of Budapest let t = tz.localize("Europe/Budapest", date(1880, 3, 1).at(12, 0, 0, 0), AmbiguityPolicy::Reject).unwrap(); assert_eq!((t.utc_offset.as_str(), t.abbreviation.as_str()), ("+01:16:20", "LMT")); } #[test] fn backzone_keeps_national_history() { let tz = &atlas().zones; // Norway had summer time 1959-1965, Germany did not (1950-1979). let oslo = tz.localize("Europe/Oslo", date(1960, 7, 1).at(12, 0, 0, 0), AmbiguityPolicy::Reject).unwrap(); let berlin = tz.localize("Europe/Berlin", date(1960, 7, 1).at(12, 0, 0, 0), AmbiguityPolicy::Reject).unwrap(); assert!(oslo.dst && !berlin.dst, "oslo {oslo:?} berlin {berlin:?}"); } #[test] fn dst_gaps_and_folds() { let tz = &atlas().zones; // 2023-10-29 02:30 happened twice in Budapest let fold = date(2023, 10, 29).at(2, 30, 0, 0); assert!(matches!( tz.localize("Europe/Budapest", fold, AmbiguityPolicy::Reject), Err(GeoError::AmbiguousTime { .. }) )); let early = tz.localize("Europe/Budapest", fold, AmbiguityPolicy::Earlier).unwrap(); let late = tz.localize("Europe/Budapest", fold, AmbiguityPolicy::Later).unwrap(); assert_eq!((early.utc_offset.as_str(), late.utc_offset.as_str()), ("+02:00", "+01:00")); assert_eq!(early.transition.as_ref().unwrap().kind, TransitionKind::Fold); // 2023-03-26 02:30 did not exist let gap = date(2023, 3, 26).at(2, 30, 0, 0); assert!(matches!( tz.localize("Europe/Budapest", gap, AmbiguityPolicy::Reject), Err(GeoError::NonexistentTime { .. }) )); let t = tz.localize("Europe/Budapest", gap, AmbiguityPolicy::Earlier).unwrap(); assert_eq!(t.transition.unwrap().kind, TransitionKind::Gap); } #[test] fn fixed_offset() { let t = atlas().zones.localize_fixed(date(1944, 5, 1).at(10, 0, 0, 0), 7200).unwrap(); assert_eq!(t.utc, "1944-05-01T08:00:00Z"); assert_eq!(t.zone, None); }