//! A time zone's main city — how Cotton fills in a person's city when all it //! has for them is their zone. //! //! A person's clock used to be chosen as a ZONE; it is chosen as a CITY now, //! and the zone comes from the place the owner picked. People who were given a //! zone before that have a clock and no place, and Cotton fills the place in by //! itself: Europe/London → London. This file is the whole of that mapping and //! it is PURE — derived from the zone identifier alone, never from a network //! call, so every device answers the same city for the same zone. //! //! Three rules, in order: //! //! 1. **A zone this build does not know is no city.** The identifier must name //! a zone in the bundled tz database; anything else ("Middle/Earth", a typo, //! free text) answers nothing rather than a guess. //! 2. **A renamed zone answers the new name.** `Asia/Calcutta` is the same //! clock as `Asia/Kolkata` under the city's old name, so it fills in //! "Kolkata"; the non-geographic spellings (`US/Eastern`, `GB`, `Japan`) //! resolve to the zone they are links to. An old identifier that names a //! DIFFERENT real place (`Asia/Tel_Aviv`, `America/Montreal`) is NOT in the //! table: the name already is somewhere a person lives, and replacing it with //! the zone it was merged into would move them. //! 3. **The last part of a geographic zone is its city**, underscores read as //! spaces (`America/Argentina/Buenos_Aires` → "Buenos Aires"), except where //! the tz database named the zone after a country or an island rather than a //! town (`Asia/Qatar`, `Atlantic/Canary`) — those answer the place's main //! town from a table. A zone outside the geographic areas (`Etc/UTC`, `UTC`, //! `EST5EDT`) has no city at all. use std::str::FromStr; use chrono_tz::Tz; /// The tz database's geographic areas — the first part of every zone that is /// named after a place. pub const GEOGRAPHIC_AREAS: [&str; 10] = [ "Africa", "America", "Antarctica", "Arctic", "Asia", "Atlantic", "Australia", "Europe", "Indian", "Pacific", ]; /// Old identifiers that are the SAME place under an older name or path, and /// the non-geographic spellings, each with the zone it now is (rule 2). pub const RENAMED_ZONES: &[(&str, &str)] = &[ // The city renamed, or the zone moved under a country. ("Africa/Asmera", "Africa/Asmara"), ("America/Buenos_Aires", "America/Argentina/Buenos_Aires"), ("America/Catamarca", "America/Argentina/Catamarca"), ( "America/Argentina/ComodRivadavia", "America/Argentina/Catamarca", ), ("America/Cordoba", "America/Argentina/Cordoba"), ("America/Fort_Wayne", "America/Indiana/Indianapolis"), ("America/Godthab", "America/Nuuk"), ("America/Indianapolis", "America/Indiana/Indianapolis"), ("America/Jujuy", "America/Argentina/Jujuy"), ("America/Knox_IN", "America/Indiana/Knox"), ("America/Louisville", "America/Kentucky/Louisville"), ("America/Mendoza", "America/Argentina/Mendoza"), ("America/Porto_Acre", "America/Rio_Branco"), ("America/Virgin", "America/St_Thomas"), ("Asia/Ashkhabad", "Asia/Ashgabat"), ("Asia/Calcutta", "Asia/Kolkata"), ("Asia/Dacca", "Asia/Dhaka"), ("Asia/Katmandu", "Asia/Kathmandu"), ("Asia/Macao", "Asia/Macau"), ("Asia/Rangoon", "Asia/Yangon"), ("Asia/Saigon", "Asia/Ho_Chi_Minh"), ("Asia/Thimbu", "Asia/Thimphu"), ("Asia/Ujung_Pandang", "Asia/Makassar"), ("Asia/Ulan_Bator", "Asia/Ulaanbaatar"), ("Atlantic/Faeroe", "Atlantic/Faroe"), ("Australia/ACT", "Australia/Sydney"), ("Australia/Canberra", "Australia/Sydney"), ("Australia/LHI", "Australia/Lord_Howe"), ("Australia/NSW", "Australia/Sydney"), ("Australia/North", "Australia/Darwin"), ("Australia/Queensland", "Australia/Brisbane"), ("Australia/South", "Australia/Adelaide"), ("Australia/Tasmania", "Australia/Hobart"), ("Australia/Victoria", "Australia/Melbourne"), ("Australia/West", "Australia/Perth"), ("Australia/Yancowinna", "Australia/Broken_Hill"), ("Europe/Kiev", "Europe/Kyiv"), ("Pacific/Enderbury", "Pacific/Kanton"), ("Pacific/Ponape", "Pacific/Pohnpei"), ("Pacific/Samoa", "Pacific/Pago_Pago"), ("Pacific/Truk", "Pacific/Chuuk"), // The non-geographic spellings: a country, a state, a legacy abbreviation. ("Brazil/Acre", "America/Rio_Branco"), ("Brazil/DeNoronha", "America/Noronha"), ("Brazil/East", "America/Sao_Paulo"), ("Brazil/West", "America/Manaus"), ("Canada/Atlantic", "America/Halifax"), ("Canada/Central", "America/Winnipeg"), ("Canada/Eastern", "America/Toronto"), ("Canada/Mountain", "America/Edmonton"), ("Canada/Newfoundland", "America/St_Johns"), ("Canada/Pacific", "America/Vancouver"), ("Canada/Saskatchewan", "America/Regina"), ("Canada/Yukon", "America/Whitehorse"), ("Chile/Continental", "America/Santiago"), ("Chile/EasterIsland", "Pacific/Easter"), ("Cuba", "America/Havana"), ("Egypt", "Africa/Cairo"), ("Eire", "Europe/Dublin"), ("GB", "Europe/London"), ("GB-Eire", "Europe/London"), ("Hongkong", "Asia/Hong_Kong"), ("Iceland", "Atlantic/Reykjavik"), ("Iran", "Asia/Tehran"), ("Israel", "Asia/Jerusalem"), ("Jamaica", "America/Jamaica"), ("Japan", "Asia/Tokyo"), ("Kwajalein", "Pacific/Kwajalein"), ("Libya", "Africa/Tripoli"), ("Mexico/BajaNorte", "America/Tijuana"), ("Mexico/BajaSur", "America/Mazatlan"), ("Mexico/General", "America/Mexico_City"), ("NZ", "Pacific/Auckland"), ("NZ-CHAT", "Pacific/Chatham"), ("Navajo", "America/Denver"), ("PRC", "Asia/Shanghai"), ("Poland", "Europe/Warsaw"), ("Portugal", "Europe/Lisbon"), ("ROC", "Asia/Taipei"), ("ROK", "Asia/Seoul"), ("Singapore", "Asia/Singapore"), ("Turkey", "Europe/Istanbul"), ("US/Alaska", "America/Anchorage"), ("US/Aleutian", "America/Adak"), ("US/Arizona", "America/Phoenix"), ("US/Central", "America/Chicago"), ("US/East-Indiana", "America/Indiana/Indianapolis"), ("US/Eastern", "America/New_York"), ("US/Hawaii", "Pacific/Honolulu"), ("US/Indiana-Starke", "America/Indiana/Knox"), ("US/Michigan", "America/Detroit"), ("US/Mountain", "America/Denver"), ("US/Pacific", "America/Los_Angeles"), ("US/Samoa", "Pacific/Pago_Pago"), ("W-SU", "Europe/Moscow"), ]; /// Geographic zones whose last part is not the name of a town — a country, an /// island or a territory — each with the place's main town (rule 3), and the /// few whose town is spelled differently from the identifier. pub const MAIN_TOWNS: &[(&str, &str)] = &[ ("Africa/El_Aaiun", "Laayoune"), ("America/Anguilla", "The Valley"), ("America/Antigua", "St. John's"), ("America/Aruba", "Oranjestad"), ("America/Barbados", "Bridgetown"), ("America/Belize", "Belize City"), ("America/Cayman", "George Town"), ("America/Costa_Rica", "San José"), ("America/Curacao", "Willemstad"), ("America/Dominica", "Roseau"), ("America/El_Salvador", "San Salvador"), ("America/Grand_Turk", "Cockburn Town"), ("America/Grenada", "St. George's"), ("America/Guadeloupe", "Pointe-à-Pitre"), ("America/Guatemala", "Guatemala City"), ("America/Guyana", "Georgetown"), ("America/Jamaica", "Kingston"), ("America/Martinique", "Fort-de-France"), ("America/Montserrat", "Brades"), ("America/Panama", "Panama City"), ("America/Puerto_Rico", "San Juan"), ("America/St_Barthelemy", "Gustavia"), ("America/St_Johns", "St. John's"), ("America/St_Kitts", "Basseterre"), ("America/St_Lucia", "Castries"), ("America/St_Thomas", "Charlotte Amalie"), ("America/St_Vincent", "Kingstown"), ("America/Tortola", "Road Town"), ("Antarctica/DumontDUrville", "Dumont d'Urville"), ("Antarctica/Macquarie", "Macquarie Island"), ("Asia/Bahrain", "Manama"), ("Asia/Brunei", "Bandar Seri Begawan"), ("Asia/Ho_Chi_Minh", "Ho Chi Minh City"), ("Asia/Kuwait", "Kuwait City"), ("Asia/Qatar", "Doha"), ("Atlantic/Azores", "Ponta Delgada"), ("Atlantic/Bermuda", "Hamilton"), ("Atlantic/Canary", "Las Palmas"), ("Atlantic/Cape_Verde", "Praia"), ("Atlantic/Faroe", "Tórshavn"), ("Atlantic/Madeira", "Funchal"), ("Atlantic/South_Georgia", "Grytviken"), ("Atlantic/St_Helena", "Jamestown"), ("Europe/Andorra", "Andorra la Vella"), ("Europe/Guernsey", "St Peter Port"), ("Europe/Isle_of_Man", "Douglas"), ("Europe/Jersey", "St Helier"), ("Europe/Malta", "Valletta"), ("Europe/Vatican", "Vatican City"), ("Indian/Chagos", "Diego Garcia"), ("Indian/Christmas", "Flying Fish Cove"), ("Indian/Cocos", "West Island"), ("Indian/Comoro", "Moroni"), ("Indian/Kerguelen", "Port-aux-Français"), ("Indian/Mahe", "Victoria"), ("Indian/Maldives", "Malé"), ("Indian/Mauritius", "Port Louis"), ("Indian/Mayotte", "Mamoudzou"), ("Indian/Reunion", "Saint-Denis"), ("Pacific/Bougainville", "Arawa"), ("Pacific/Chatham", "Waitangi"), ("Pacific/Chuuk", "Weno"), ("Pacific/Easter", "Hanga Roa"), ("Pacific/Efate", "Port Vila"), ("Pacific/Fiji", "Suva"), ("Pacific/Galapagos", "Puerto Ayora"), ("Pacific/Gambier", "Rikitea"), ("Pacific/Guadalcanal", "Honiara"), ("Pacific/Guam", "Hagåtña"), ("Pacific/Kosrae", "Tofol"), ("Pacific/Marquesas", "Taiohae"), ("Pacific/Nauru", "Yaren"), ("Pacific/Niue", "Alofi"), ("Pacific/Norfolk", "Kingston"), ("Pacific/Palau", "Ngerulmud"), ("Pacific/Pitcairn", "Adamstown"), ("Pacific/Pohnpei", "Palikir"), ("Pacific/Rarotonga", "Avarua"), ("Pacific/Tahiti", "Papeete"), ("Pacific/Tongatapu", "Nuku'alofa"), ("Pacific/Wake", "Wake Island"), ("Pacific/Wallis", "Mata-Utu"), ]; /// The zone an identifier is now: itself, or what a renamed or /// non-geographic spelling resolves to (rule 2). Unknown text is returned as /// it came; [`main_city`] is the function that refuses it. pub fn current_zone(zone: &str) -> &str { let zone = zone.trim(); RENAMED_ZONES .iter() .find(|(old, _)| *old == zone) .map_or(zone, |(_, now)| now) } /// The main city of a time zone, or nothing when the zone names no place. /// /// `Europe/London` → "London"; `Asia/Calcutta` → "Kolkata"; /// `America/Argentina/Buenos_Aires` → "Buenos Aires"; `Etc/UTC` → nothing; /// an identifier this build's tz database does not know → nothing. pub fn main_city(zone: &str) -> Option { let zone = zone.trim(); // Rule 1: a zone nobody can place a clock in is no evidence of a place. Tz::from_str(zone).ok()?; let zone = current_zone(zone); if let Some((_, town)) = MAIN_TOWNS.iter().find(|(id, _)| *id == zone) { return Some((*town).to_string()); } let mut parts = zone.split('/'); let area = parts.next()?; if !GEOGRAPHIC_AREAS.contains(&area) { return None; } let last = zone.rsplit('/').next()?; if last == area { return None; } let city = last.replace('_', " "); let city = city.trim(); (!city.is_empty()).then(|| city.to_string()) } #[cfg(test)] #[path = "zone_city_tests.rs"] mod tests;