iso8601/
time.rs

1use alloc::string::String;
2use core::str::FromStr;
3
4use crate::parsers;
5
6/// A time object.
7/// ```
8/// # use std::str::FromStr;
9/// assert_eq!(
10///     iso8601::Time::from_str("17:08:08.793Z"),
11///     Ok(iso8601::Time{ hour: 17, minute: 8, second: 8, millisecond: 793, tz_offset_hours: 0, tz_offset_minutes: 00 })
12/// )
13/// ```
14#[derive(Eq, PartialEq, Debug, Copy, Clone, Default)]
15pub struct Time {
16    /// a 24th of a day
17    pub hour: u32,
18    /// 60 discrete parts of an hour
19    pub minute: u32,
20    /// a minute are 60 of these
21    pub second: u32,
22    /// everything after a `.`
23    pub millisecond: u32,
24    /// the hour part of the timezone offset from UTC
25    pub tz_offset_hours: i32,
26    /// the minute part of the timezone offset from UTC
27    pub tz_offset_minutes: i32,
28}
29
30impl Time {
31    /// Change this time's timezone offset.
32    ///
33    /// # Arguments
34    ///
35    /// * `tzo` - A tuple of `(hours, minutes)` specifying the timezone offset from UTC.
36    pub fn set_tz(&self, tzo: (i32, i32)) -> Time {
37        let mut t = *self;
38        t.tz_offset_hours = tzo.0;
39        t.tz_offset_minutes = tzo.1;
40        t
41    }
42}
43
44impl FromStr for Time {
45    type Err = String;
46
47    fn from_str(s: &str) -> Result<Self, Self::Err> {
48        time(s)
49    }
50}
51
52/// Parses a time string.
53///
54/// A string can have one of the following formats:
55///
56/// * `07:35:[00][.123]` or `0735[00][.123]`
57/// * `07:35:[00][.123][(Z|(+|-)00:00)]`
58/// * `0735[00][.123][(Z|(+|-)00:00)]`
59/// * `0735[00][.123][(Z|(+|-)0000)]`
60///
61/// ## Example
62///
63/// ```rust
64/// let time = iso8601::time("21:56:42").unwrap();
65/// ```
66pub fn time(string: &str) -> Result<Time, String> {
67    if let Ok((_, parsed)) = parsers::parse_time(string.as_bytes()) {
68        Ok(parsed)
69    } else {
70        Err(format!("Failed to parse time: {}", string))
71    }
72}