Parse timezone string into either a named timezone or fixed offset
(tz_str: &str)
| 17 | |
| 18 | /// Parse timezone string into either a named timezone or fixed offset |
| 19 | fn parse_timezone(tz_str: &str) -> Result<TimezoneType, String> { |
| 20 | // Try parsing as named timezone first |
| 21 | if let Ok(tz) = tz_str.parse::<Tz>() { |
| 22 | return Ok(TimezoneType::Named(tz)); |
| 23 | } |
| 24 | |
| 25 | // Try common timezone abbreviations |
| 26 | let canonical_tz = match tz_str.to_uppercase().as_str() { |
| 27 | "UTC" | "GMT" => "UTC", |
| 28 | "EST" => "America/New_York", // Eastern Standard Time |
| 29 | "EDT" => "America/New_York", // Eastern Daylight Time |
| 30 | "CST" => "America/Chicago", // Central Standard Time |
| 31 | "CDT" => "America/Chicago", // Central Daylight Time |
| 32 | "MST" => "America/Denver", // Mountain Standard Time |
| 33 | "MDT" => "America/Denver", // Mountain Daylight Time |
| 34 | "PST" => "America/Los_Angeles", // Pacific Standard Time |
| 35 | "PDT" => "America/Los_Angeles", // Pacific Daylight Time |
| 36 | "BST" => "Europe/London", // British Summer Time |
| 37 | "CET" => "Europe/Paris", // Central European Time |
| 38 | "CEST" => "Europe/Paris", // Central European Summer Time |
| 39 | "JST" => "Asia/Tokyo", // Japan Standard Time |
| 40 | "IST" => "Asia/Kolkata", // India Standard Time |
| 41 | "AEST" => "Australia/Sydney", // Australian Eastern Standard Time |
| 42 | "AEDT" => "Australia/Sydney", // Australian Eastern Daylight Time |
| 43 | _ => tz_str, // Use original if no abbreviation match |
| 44 | }; |
| 45 | |
| 46 | // Try parsing the canonical timezone name |
| 47 | if let Ok(tz) = canonical_tz.parse::<Tz>() { |
| 48 | return Ok(TimezoneType::Named(tz)); |
| 49 | } |
| 50 | |
| 51 | // Try parsing as fixed offset (+05:30, -04:00, etc.) |
| 52 | if let Ok(offset) = parse_fixed_offset(tz_str) { |
| 53 | return Ok(TimezoneType::Fixed(offset)); |
| 54 | } |
| 55 | |
| 56 | Err(format!("Invalid timezone: {}", tz_str)) |
| 57 | } |
| 58 | |
| 59 | /// Parse fixed offset strings like "+05:30", "-04:00", "+0530", "-0400" |
| 60 | fn parse_fixed_offset(offset_str: &str) -> Result<FixedOffset, String> { |
no test coverage detected