use alloc::string::String;
use core::str::FromStr;
use crate::parsers;
#[derive(Eq, PartialEq, Debug, Copy, Clone, Default)]
pub struct Time {
pub hour: u32,
pub minute: u32,
pub second: u32,
pub millisecond: u32,
pub tz_offset_hours: i32,
pub tz_offset_minutes: i32,
}
impl Time {
pub fn set_tz(&self, tzo: (i32, i32)) -> Time {
let mut t = *self;
t.tz_offset_hours = tzo.0;
t.tz_offset_minutes = tzo.1;
t
}
}
impl FromStr for Time {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
time(s)
}
}
pub fn time(string: &str) -> Result<Time, String> {
if let Ok((_, parsed)) = parsers::parse_time(string.as_bytes()) {
Ok(parsed)
} else {
Err(format!("Failed to parse time: {}", string))
}
}