Parses `(UTC)`, `(UTC+2)`, `(UTC-7)`, or `(UTC+5:30)` into offset seconds.
(zone: &str)
| 258 | |
| 259 | /// Parses `(UTC)`, `(UTC+2)`, `(UTC-7)`, or `(UTC+5:30)` into offset seconds. |
| 260 | fn parse_utc_offset(zone: &str) -> Option<i64> { |
| 261 | let inner = zone.strip_prefix("(UTC")?.strip_suffix(')')?; |
| 262 | if inner.is_empty() { |
| 263 | return Some(0); |
| 264 | } |
| 265 | let (sign, magnitude) = match inner.as_bytes().first()? { |
| 266 | b'+' => (1, &inner[1..]), |
| 267 | b'-' => (-1, &inner[1..]), |
| 268 | _ => return None, |
| 269 | }; |
| 270 | let (hours_text, minutes_text) = magnitude.split_once(':').unwrap_or((magnitude, "0")); |
| 271 | let hours: i64 = hours_text.parse().ok()?; |
| 272 | let minutes: i64 = minutes_text.parse().ok()?; |
| 273 | if hours > 23 || minutes > 59 { |
| 274 | return None; |
| 275 | } |
| 276 | Some(sign * (hours * 3_600 + minutes * 60)) |
| 277 | } |
| 278 | |
| 279 | fn parse_fixed_i32(value: &str, start: usize, end: usize) -> Option<i32> { |
| 280 | value.get(start..end)?.parse().ok() |
no test coverage detected