use chrono::{FixedOffset};
use chrono_tz::Tz;
use crate::error::TimeParseError;
use crate::types::TimeZoneParsed;
pub fn parse_timezone_str(tz_str: &str) -> Result<TimeZoneParsed, TimeParseError> {
let tz_str = tz_str.trim();
if tz_str.eq_ignore_ascii_case("UTC") {
return FixedOffset::east_opt(0)
.map(TimeZoneParsed::FixedOffset)
.ok_or_else(|| TimeParseError::InvalidInput("Invalid UTC offset".into()));
}
if tz_str.starts_with('+') || tz_str.starts_with('-') {
if let Ok(offset) = tz_str.parse::<FixedOffset>() {
return Ok(TimeZoneParsed::FixedOffset(offset));
} else {
return Err(TimeParseError::InvalidInput(format!(
"Invalid fixed offset format: '{}'", tz_str
)));
}
}
if !tz_str.contains('/') || tz_str.starts_with('/') || tz_str.ends_with('/') {
return Err(TimeParseError::InvalidInput(format!(
"Invalid IANA timezone format: '{}'", tz_str
)));
}
match tz_str.parse::<Tz>() {
Ok(tz) => Ok(TimeZoneParsed::Iana(tz)),
Err(e) => Err(TimeParseError::InvalidInput(format!(
"Unknown IANA timezone '{}': {}", tz_str, e
))),
}
}