Files
base64
bitflags
bytes
cfg_if
dtoa
encoding_rs
fnv
foreign_types
foreign_types_shared
futures
futures_channel
futures_core
futures_executor
futures_io
futures_macro
futures_sink
futures_task
futures_util
async_await
future
io
lock
sink
stream
task
goauth
h2
hashbrown
http
http_body
httparse
hyper
hyper_tls
idna
indexmap
iovec
itoa
lazy_static
libc
log
matches
memchr
mime
mime_guess
mio
native_tls
net2
num_cpus
once_cell
openssl
openssl_probe
openssl_sys
percent_encoding
pin_project
pin_project_internal
pin_project_lite
pin_utils
proc_macro2
proc_macro_hack
proc_macro_nested
quote
reqwest
ryu
serde
serde_derive
serde_json
serde_urlencoded
simpl
slab
smpl_jwt
socket2
standback
syn
time
time_macros
time_macros_impl
tinyvec
tokio
future
io
loom
macros
net
park
runtime
stream
sync
task
time
util
tokio_tls
tokio_util
tower_service
tracing
tracing_core
try_lock
unicase
unicode_bidi
unicode_normalization
unicode_xid
url
want
  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
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//! Formatting and parsing for well-known formats (typically RFCs).

use crate::{
    format::{
        date,
        parse::{
            try_consume_char, try_consume_char_case_insensitive, try_consume_exact_digits,
            try_consume_exact_digits_in_range, try_consume_first_match,
        },
        time, Padding, ParseResult, ParsedItems,
    },
    internal_prelude::*,
};
use core::fmt::{self, Formatter};

/// The format as specified by RFC3339.
pub(crate) mod rfc3339 {
    use super::*;

    /// Format `df` according to the RFC3339 specification.
    #[inline]
    pub(crate) fn fmt(df: &DeferredFormat, f: &mut Formatter<'_>) -> fmt::Result {
        // If we're using RFC3339, all three components must be present.
        // This will be enforced with typestate when Rust gains sufficient
        // capabilities (namely proper sealed traits and/or function overloading).
        #[allow(clippy::option_unwrap_used)]
        let date = df.date().unwrap();
        #[allow(clippy::option_unwrap_used)]
        let time = df.time().unwrap();
        #[allow(clippy::option_unwrap_used)]
        let offset = df.offset().unwrap();

        date::fmt_Y(f, date, Padding::Zero)?;
        f.write_str("-")?;
        date::fmt_m(f, date, Padding::Zero)?;
        f.write_str("-")?;
        date::fmt_d(f, date, Padding::Zero)?;
        f.write_str("T")?;
        time::fmt_H(f, time, Padding::Zero)?;
        f.write_str(":")?;
        time::fmt_M(f, time, Padding::Zero)?;
        f.write_str(":")?;
        time::fmt_S(f, time, Padding::Zero)?;
        write!(
            f,
            "{:+03}:{:02}",
            offset.as_hours(),
            offset.as_minutes().rem_euclid(60)
        )?;

        Ok(())
    }

    /// Parse `s` as specified by RFC3339.
    #[inline]
    pub(crate) fn parse(items: &mut ParsedItems, s: &mut &str) -> ParseResult<()> {
        items.year = try_consume_exact_digits::<i32>(s, 4, Padding::None)
            .ok_or(ParseError::InvalidYear)?
            .into();
        try_consume_char(s, '-')?;
        date::parse_m(items, s, Padding::Zero)?;
        try_consume_char(s, '-')?;
        date::parse_d(items, s, Padding::Zero)?;
        try_consume_char_case_insensitive(s, 'T')?;
        time::parse_H(items, s, Padding::Zero)?;
        try_consume_char(s, ':')?;
        time::parse_M(items, s, Padding::Zero)?;
        try_consume_char(s, ':')?;
        time::parse_S(items, s, Padding::Zero)?;

        if try_consume_char(s, '.').is_ok() {
            let num_digits = s.chars().take_while(char::is_ascii_digit).count();
            if num_digits == 0 {
                return Err(ParseError::InvalidNanosecond);
            }
            let num_digits_used = core::cmp::min(num_digits, 9);

            let nanos_raw: String = s.chars().take(num_digits_used).collect();
            // At most 9 decimal digits will always fit in a u32.
            // `num_digits_used` is at most 9, which can safely be cast.
            #[allow(clippy::result_unwrap_used)]
            let nanos = nanos_raw.parse::<u32>().unwrap() * 10_u32.pow(9 - num_digits_used as u32);
            items.nanosecond = Some(nanos);
            *s = &s[num_digits..];
        }

        if try_consume_char_case_insensitive(s, 'Z').is_ok() {
            items.offset = Some(UtcOffset::UTC);
        } else {
            let offset_sign =
                match try_consume_first_match(s, [("+", 1), ("-", -1)].iter().cloned()) {
                    Some(sign) => sign,
                    None => {
                        return Err(match s.chars().next() {
                            Some(actual) => ParseError::UnexpectedCharacter {
                                actual,
                                expected: '+',
                            },
                            None => ParseError::UnexpectedEndOfString,
                        })
                    }
                };
            let offset_hour = try_consume_exact_digits_in_range(s, 2, 0..=23, Padding::Zero)
                .ok_or(ParseError::InvalidOffset)?;
            try_consume_char(s, ':')?;
            let offset_minute = try_consume_exact_digits_in_range(s, 2, 0..=59, Padding::Zero)
                .ok_or(ParseError::InvalidOffset)?;
            items.offset = Some(UtcOffset::seconds(
                offset_sign * (offset_hour * 60 + offset_minute),
            ));
        }

        Ok(())
    }
}