1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//! Formatting helpers for a `UtcOffset`.

#![allow(non_snake_case)]

use crate::{
    error,
    format::{
        parse::{try_consume_exact_digits, try_consume_first_match},
        Padding, ParsedItems,
    },
    ParseResult, UtcOffset,
};
use core::fmt::{self, Formatter};

/// UTC offset
pub(crate) fn fmt_z(f: &mut Formatter<'_>, offset: UtcOffset) -> fmt::Result {
    let offset = offset.as_duration();

    write!(
        f,
        "{}{:02}{:02}",
        if offset.is_negative() { '-' } else { '+' },
        offset.whole_hours().abs(),
        (offset.whole_minutes() - 60 * offset.whole_hours()).abs()
    )
}

/// UTC offset
pub(crate) fn parse_z(items: &mut ParsedItems, s: &mut &str) -> ParseResult<()> {
    let sign = try_consume_first_match(s, [("+", 1), ("-", -1)].iter().cloned())
        .ok_or(error::Parse::InvalidOffset)?;

    let hours: i16 =
        try_consume_exact_digits(s, 2, Padding::Zero).ok_or(error::Parse::InvalidOffset)?;

    let minutes: i16 =
        try_consume_exact_digits(s, 2, Padding::Zero).ok_or(error::Parse::InvalidOffset)?;

    items.offset = UtcOffset::minutes(sign * (hours * 60 + minutes)).into();
    Ok(())
}