| 1 | use chrono_tz::Tz; |
| 2 | |
| 3 | /// Resolve a TZID to chrono-tz, including common Windows zone names from Outlook iMIP. |
| 4 | pub fn resolve_tzid(tzid: &str) -> Option<Tz> { |
| 5 | let trimmed = tzid.trim().trim_matches('"'); |
| 6 | if trimmed.is_empty() { |
| 7 | return None; |
| 8 | } |
| 9 | if let Ok(tz) = trimmed.parse::<Tz>() { |
| 10 | return Some(tz); |
| 11 | } |
| 12 | windows_to_iana(trimmed).and_then(|iana| iana.parse().ok()) |
| 13 | } |
| 14 | |
| 15 | fn windows_to_iana(name: &str) -> Option<&'static str> { |
| 16 | Some(match name { |
| 17 | "W. Europe Standard Time" => "Europe/Berlin", |
| 18 | "Central European Standard Time" => "Europe/Warsaw", |
| 19 | "Romance Standard Time" => "Europe/Paris", |
| 20 | "GMT Standard Time" => "Europe/London", |
| 21 | "UTC" | "Coordinated Universal Time" => "UTC", |
| 22 | "Pacific Standard Time" => "America/Los_Angeles", |
| 23 | "Mountain Standard Time" => "America/Denver", |
| 24 | "Central Standard Time" => "America/Chicago", |
| 25 | "Eastern Standard Time" => "America/New_York", |
| 26 | "Tokyo Standard Time" => "Asia/Tokyo", |
| 27 | "China Standard Time" => "Asia/Shanghai", |
| 28 | "AUS Eastern Standard Time" => "Australia/Sydney", |
| 29 | "Russian Standard Time" => "Europe/Moscow", |
| 30 | "India Standard Time" => "Asia/Kolkata", |
| 31 | _ => return None, |
| 32 | }) |
| 33 | } |
| 34 | |
| 35 | #[cfg(test)] |
| 36 | mod tests { |
| 37 | use super::*; |
| 38 | |
| 39 | #[test] |
| 40 | fn maps_windows_w_europe() { |
| 41 | assert_eq!( |
| 42 | resolve_tzid("W. Europe Standard Time"), |
| 43 | Some(chrono_tz::Europe::Berlin) |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | #[test] |
| 48 | fn parses_iana() { |
| 49 | assert_eq!( |
| 50 | resolve_tzid("Europe/Zurich"), |
| 51 | Some(chrono_tz::Europe::Zurich) |
| 52 | ); |
| 53 | } |
| 54 | } |