use crate::{
error::{fmt::offset::Error as E, Error, ErrorContext},
fmt::{
buffer::ArrayBuffer,
temporal::{PiecesNumericOffset, PiecesOffset},
util::parse_temporal_fraction,
Parsed,
},
tz::Offset,
util::{b, parse},
};
#[derive(Debug)]
pub(crate) struct ParsedOffset {
kind: ParsedOffsetKind,
}
impl ParsedOffset {
pub(crate) fn to_offset(&self) -> Result<Offset, Error> {
match self.kind {
ParsedOffsetKind::Zulu => Ok(Offset::UTC),
ParsedOffsetKind::Numeric(ref numeric) => numeric.to_offset(),
}
}
pub(crate) fn to_pieces_offset(&self) -> Result<PiecesOffset, Error> {
match self.kind {
ParsedOffsetKind::Zulu => Ok(PiecesOffset::Zulu),
ParsedOffsetKind::Numeric(ref numeric) => {
let mut off = PiecesNumericOffset::from(numeric.to_offset()?);
if numeric.sign.is_negative() {
off = off.with_negative_zero();
}
Ok(PiecesOffset::from(off))
}
}
}
pub(crate) fn is_zulu(&self) -> bool {
matches!(self.kind, ParsedOffsetKind::Zulu)
}
pub(crate) fn has_subminute(&self) -> bool {
let ParsedOffsetKind::Numeric(ref numeric) = self.kind else {
return false;
};
numeric.seconds.is_some()
}
}
impl core::fmt::Display for ParsedOffset {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self.kind {
ParsedOffsetKind::Zulu => f.write_str("Z"),
ParsedOffsetKind::Numeric(ref numeric) => {
core::fmt::Display::fmt(numeric, f)
}
}
}
}
#[derive(Debug)]
enum ParsedOffsetKind {
Zulu,
Numeric(Numeric),
}
struct Numeric {
sign: b::Sign,
hours: i8,
minutes: Option<i8>,
seconds: Option<i8>,
nanoseconds: Option<i32>,
}
impl Numeric {
fn to_offset(&self) -> Result<Offset, Error> {
let mut seconds = i32::from(self.hours) * b::SECS_PER_HOUR_32;
if let Some(part_minutes) = self.minutes {
seconds += i32::from(part_minutes) * b::SECS_PER_MIN_32;
}
if let Some(part_seconds) = self.seconds {
seconds += i32::from(part_seconds);
}
if let Some(part_nanoseconds) = self.nanoseconds {
if part_nanoseconds >= 500_000_000 {
seconds += 1;
}
}
Ok(Offset::from_seconds(self.sign * seconds)
.map_err(|_| E::PrecisionLoss)?)
}
}
impl core::fmt::Display for Numeric {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let mut buf = ArrayBuffer::<19>::default();
let mut bbuf = buf.as_borrowed();
bbuf.write_ascii_char(if self.sign.is_negative() {
b'-'
} else {
b'+'
});
bbuf.write_int_pad2(self.hours.unsigned_abs());
if let Some(minutes) = self.minutes {
bbuf.write_ascii_char(b':');
bbuf.write_int_pad2(minutes.unsigned_abs());
}
if let Some(seconds) = self.seconds {
if self.minutes.is_none() {
bbuf.write_str(":00");
}
bbuf.write_ascii_char(b':');
bbuf.write_int_pad2(seconds.unsigned_abs());
}
if let Some(nanos) = self.nanoseconds {
if nanos != 0 {
if self.minutes.is_none() {
bbuf.write_str(":00");
}
if self.seconds.is_none() {
bbuf.write_str(":00");
}
bbuf.write_ascii_char(b'.');
bbuf.write_fraction(None, nanos.unsigned_abs());
}
}
f.write_str(bbuf.filled())
}
}
impl core::fmt::Debug for Numeric {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
core::fmt::Display::fmt(self, f)
}
}
#[derive(Debug)]
pub(crate) struct Parser {
zulu: bool,
require_minute: bool,
require_second: bool,
subminute: bool,
subsecond: bool,
colon: Colon,
}
impl Parser {
pub(crate) const fn new() -> Parser {
Parser {
zulu: true,
require_minute: false,
require_second: false,
subminute: true,
subsecond: true,
colon: Colon::Optional,
}
}
pub(crate) const fn zulu(self, yes: bool) -> Parser {
Parser { zulu: yes, ..self }
}
pub(crate) const fn require_minute(self, yes: bool) -> Parser {
Parser { require_minute: yes, ..self }
}
pub(crate) const fn require_second(self, yes: bool) -> Parser {
Parser { require_second: yes, ..self }
}
pub(crate) const fn subminute(self, yes: bool) -> Parser {
Parser { subminute: yes, ..self }
}
pub(crate) const fn subsecond(self, yes: bool) -> Parser {
Parser { subsecond: yes, ..self }
}
pub(crate) const fn colon(self, colon: Colon) -> Parser {
Parser { colon, ..self }
}
pub(crate) fn parse<'i>(
&self,
mut input: &'i [u8],
) -> Result<Parsed<'i, ParsedOffset>, Error> {
if input.is_empty() {
return Err(Error::from(E::EndOfInput));
}
if input[0] == b'Z' || input[0] == b'z' {
if !self.zulu {
return Err(Error::from(E::UnexpectedLetterOffsetNoZulu(
input[0],
)));
}
input = &input[1..];
let value = ParsedOffset { kind: ParsedOffsetKind::Zulu };
return Ok(Parsed { value, input });
}
let Parsed { value: numeric, input } = self.parse_numeric(input)?;
let value = ParsedOffset { kind: ParsedOffsetKind::Numeric(numeric) };
Ok(Parsed { value, input })
}
#[cfg_attr(feature = "perf-inline", inline(always))]
pub(crate) fn parse_optional<'i>(
&self,
input: &'i [u8],
) -> Result<Parsed<'i, Option<ParsedOffset>>, Error> {
let Some(first) = input.first().copied() else {
return Ok(Parsed { value: None, input });
};
if !matches!(first, b'z' | b'Z' | b'+' | b'-') {
return Ok(Parsed { value: None, input });
}
let Parsed { value, input } = self.parse(input)?;
Ok(Parsed { value: Some(value), input })
}
#[cfg_attr(feature = "perf-inline", inline(always))]
fn parse_numeric<'i>(
&self,
input: &'i [u8],
) -> Result<Parsed<'i, Numeric>, Error> {
let Parsed { value: sign, input } =
self.parse_sign(input).context(E::InvalidSign)?;
let Parsed { value: hours, input } =
self.parse_hours(input).context(E::InvalidHours)?;
let extended = match self.colon {
Colon::Optional => input.starts_with(b":"),
Colon::Required => {
if !input.is_empty() && !input.starts_with(b":") {
return Err(Error::from(E::NoColonAfterHours));
}
true
}
Colon::Absent => {
if !input.is_empty() && input.starts_with(b":") {
return Err(Error::from(E::ColonAfterHours));
}
false
}
};
let mut numeric = Numeric {
sign,
hours,
minutes: None,
seconds: None,
nanoseconds: None,
};
let Parsed { value: has_minutes, input } = self
.parse_separator(input, extended)
.context(E::SeparatorAfterHours)?;
if !has_minutes {
return if self.require_minute
|| (self.subminute && self.require_second)
{
Err(Error::from(E::MissingMinuteAfterHour))
} else {
Ok(Parsed { value: numeric, input })
};
}
let Parsed { value: minutes, input } =
self.parse_minutes(input).context(E::InvalidMinutes)?;
numeric.minutes = Some(minutes);
if !self.subminute {
return if input.get(0).map_or(false, |&b| b == b':') {
Err(Error::from(E::SubminutePrecisionNotEnabled))
} else {
Ok(Parsed { value: numeric, input })
};
}
let Parsed { value: has_seconds, input } = self
.parse_separator(input, extended)
.context(E::SeparatorAfterMinutes)?;
if !has_seconds {
return if self.require_second {
Err(Error::from(E::MissingSecondAfterMinute))
} else {
Ok(Parsed { value: numeric, input })
};
}
let Parsed { value: seconds, input } =
self.parse_seconds(input).context(E::InvalidSeconds)?;
numeric.seconds = Some(seconds);
if !self.subsecond {
if input.get(0).map_or(false, |&b| b == b'.' || b == b',') {
return Err(Error::from(E::SubsecondPrecisionNotEnabled));
}
return Ok(Parsed { value: numeric, input });
}
let Parsed { value: nanoseconds, input } =
parse_temporal_fraction(input)
.context(E::InvalidSecondsFractional)?;
numeric.nanoseconds = nanoseconds.map(|n| i32::try_from(n).unwrap());
Ok(Parsed { value: numeric, input })
}
#[cfg_attr(feature = "perf-inline", inline(always))]
fn parse_sign<'i>(
&self,
input: &'i [u8],
) -> Result<Parsed<'i, b::Sign>, Error> {
let sign = input.get(0).copied().ok_or(E::EndOfInputNumeric)?;
let sign = if sign == b'+' {
b::Sign::Positive
} else if sign == b'-' {
b::Sign::Negative
} else {
return Err(Error::from(E::InvalidSignPlusOrMinus));
};
Ok(Parsed { value: sign, input: &input[1..] })
}
#[cfg_attr(feature = "perf-inline", inline(always))]
fn parse_hours<'i>(
&self,
input: &'i [u8],
) -> Result<Parsed<'i, i8>, Error> {
let (hours, input) =
parse::split(input, 2).ok_or(E::EndOfInputHour)?;
let hours = b::OffsetHours::parse(hours).context(E::ParseHours)?;
Ok(Parsed { value: hours, input })
}
#[cfg_attr(feature = "perf-inline", inline(always))]
fn parse_minutes<'i>(
&self,
input: &'i [u8],
) -> Result<Parsed<'i, i8>, Error> {
let (minutes, input) =
parse::split(input, 2).ok_or(E::EndOfInputMinute)?;
let minutes =
b::OffsetMinutes::parse(minutes).context(E::ParseMinutes)?;
Ok(Parsed { value: minutes, input })
}
#[cfg_attr(feature = "perf-inline", inline(always))]
fn parse_seconds<'i>(
&self,
input: &'i [u8],
) -> Result<Parsed<'i, i8>, Error> {
let (seconds, input) =
parse::split(input, 2).ok_or(E::EndOfInputSecond)?;
let seconds =
b::OffsetSeconds::parse(seconds).context(E::ParseSeconds)?;
Ok(Parsed { value: seconds, input })
}
#[cfg_attr(feature = "perf-inline", inline(always))]
fn parse_separator<'i>(
&self,
mut input: &'i [u8],
extended: bool,
) -> Result<Parsed<'i, bool>, Error> {
if !extended {
let expected =
input.len() >= 2 && input[..2].iter().all(u8::is_ascii_digit);
return Ok(Parsed { value: expected, input });
}
let is_separator = input.get(0).map_or(false, |&b| b == b':');
if is_separator {
input = &input[1..];
}
Ok(Parsed { value: is_separator, input })
}
}
#[derive(Debug)]
pub(crate) enum Colon {
Optional,
Required,
Absent,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ok_zulu() {
let p = |input| Parser::new().parse(input).unwrap();
insta::assert_debug_snapshot!(p(b"Z"), @r###"
Parsed {
value: ParsedOffset {
kind: Zulu,
},
input: "",
}
"###);
insta::assert_debug_snapshot!(p(b"z"), @r###"
Parsed {
value: ParsedOffset {
kind: Zulu,
},
input: "",
}
"###);
}
#[test]
fn ok_numeric() {
let p = |input| Parser::new().parse(input).unwrap();
insta::assert_debug_snapshot!(p(b"-05"), @r###"
Parsed {
value: ParsedOffset {
kind: Numeric(
-05,
),
},
input: "",
}
"###);
}
#[test]
fn ok_numeric_complete() {
let p = |input| Parser::new().parse_numeric(input).unwrap();
insta::assert_debug_snapshot!(p(b"-05"), @r###"
Parsed {
value: -05,
input: "",
}
"###);
insta::assert_debug_snapshot!(p(b"+05"), @r###"
Parsed {
value: +05,
input: "",
}
"###);
insta::assert_debug_snapshot!(p(b"+25:59"), @r###"
Parsed {
value: +25:59,
input: "",
}
"###);
insta::assert_debug_snapshot!(p(b"+2559"), @r###"
Parsed {
value: +25:59,
input: "",
}
"###);
insta::assert_debug_snapshot!(p(b"+25:59:59"), @r###"
Parsed {
value: +25:59:59,
input: "",
}
"###);
insta::assert_debug_snapshot!(p(b"+255959"), @r###"
Parsed {
value: +25:59:59,
input: "",
}
"###);
insta::assert_debug_snapshot!(p(b"+25:59:59.999"), @r###"
Parsed {
value: +25:59:59.999,
input: "",
}
"###);
insta::assert_debug_snapshot!(p(b"+25:59:59,999"), @r###"
Parsed {
value: +25:59:59.999,
input: "",
}
"###);
insta::assert_debug_snapshot!(p(b"+255959.999"), @r###"
Parsed {
value: +25:59:59.999,
input: "",
}
"###);
insta::assert_debug_snapshot!(p(b"+255959,999"), @r###"
Parsed {
value: +25:59:59.999,
input: "",
}
"###);
insta::assert_debug_snapshot!(p(b"+25:59:59.999999999"), @r###"
Parsed {
value: +25:59:59.999999999,
input: "",
}
"###);
}
#[test]
fn ok_numeric_incomplete() {
let p = |input| Parser::new().parse_numeric(input).unwrap();
insta::assert_debug_snapshot!(p(b"-05a"), @r###"
Parsed {
value: -05,
input: "a",
}
"###);
insta::assert_debug_snapshot!(p(b"-05:12a"), @r###"
Parsed {
value: -05:12,
input: "a",
}
"###);
insta::assert_debug_snapshot!(p(b"-05:12."), @r###"
Parsed {
value: -05:12,
input: ".",
}
"###);
insta::assert_debug_snapshot!(p(b"-05:12,"), @r###"
Parsed {
value: -05:12,
input: ",",
}
"###);
insta::assert_debug_snapshot!(p(b"-0512a"), @r###"
Parsed {
value: -05:12,
input: "a",
}
"###);
insta::assert_debug_snapshot!(p(b"-0512:"), @r###"
Parsed {
value: -05:12,
input: ":",
}
"###);
insta::assert_debug_snapshot!(p(b"-05:12:34a"), @r###"
Parsed {
value: -05:12:34,
input: "a",
}
"###);
insta::assert_debug_snapshot!(p(b"-05:12:34.9a"), @r###"
Parsed {
value: -05:12:34.9,
input: "a",
}
"###);
insta::assert_debug_snapshot!(p(b"-05:12:34.9."), @r###"
Parsed {
value: -05:12:34.9,
input: ".",
}
"###);
insta::assert_debug_snapshot!(p(b"-05:12:34.9,"), @r###"
Parsed {
value: -05:12:34.9,
input: ",",
}
"###);
}
#[test]
fn err_numeric_empty() {
insta::assert_snapshot!(
Parser::new().parse_numeric(b"").unwrap_err(),
@"failed to parse sign in UTC numeric offset: expected UTC numeric offset, but found end of input",
);
}
#[test]
fn err_numeric_notsign() {
insta::assert_snapshot!(
Parser::new().parse_numeric(b"*").unwrap_err(),
@"failed to parse sign in UTC numeric offset: expected `+` or `-` sign at start of UTC numeric offset",
);
}
#[test]
fn err_numeric_hours_too_short() {
insta::assert_snapshot!(
Parser::new().parse_numeric(b"+a").unwrap_err(),
@"failed to parse hours in UTC numeric offset: expected two digit hour after sign, but found end of input",
);
}
#[test]
fn err_numeric_hours_invalid_digits() {
insta::assert_snapshot!(
Parser::new().parse_numeric(b"+ab").unwrap_err(),
@"failed to parse hours in UTC numeric offset: failed to parse hours (requires a two digit integer): invalid digit, expected 0-9 but got a",
);
}
#[test]
fn err_numeric_hours_out_of_range() {
insta::assert_snapshot!(
Parser::new().parse_numeric(b"-26").unwrap_err(),
@"failed to parse hours in UTC numeric offset: failed to parse hours (requires a two digit integer): parameter 'time zone offset hours' is not in the required range of -25..=25",
);
}
#[test]
fn err_numeric_minutes_too_short() {
insta::assert_snapshot!(
Parser::new().parse_numeric(b"+05:a").unwrap_err(),
@"failed to parse minutes in UTC numeric offset: expected two digit minute after hours, but found end of input",
);
}
#[test]
fn err_numeric_minutes_invalid_digits() {
insta::assert_snapshot!(
Parser::new().parse_numeric(b"+05:ab").unwrap_err(),
@"failed to parse minutes in UTC numeric offset: failed to parse minutes (requires a two digit integer): invalid digit, expected 0-9 but got a",
);
}
#[test]
fn err_numeric_minutes_out_of_range() {
insta::assert_snapshot!(
Parser::new().parse_numeric(b"-05:60").unwrap_err(),
@"failed to parse minutes in UTC numeric offset: failed to parse minutes (requires a two digit integer): parameter 'time zone offset minutes' is not in the required range of -59..=59",
);
}
#[test]
fn err_numeric_seconds_too_short() {
insta::assert_snapshot!(
Parser::new().parse_numeric(b"+05:30:a").unwrap_err(),
@"failed to parse seconds in UTC numeric offset: expected two digit second after minutes, but found end of input",
);
}
#[test]
fn err_numeric_seconds_invalid_digits() {
insta::assert_snapshot!(
Parser::new().parse_numeric(b"+05:30:ab").unwrap_err(),
@"failed to parse seconds in UTC numeric offset: failed to parse seconds (requires a two digit integer): invalid digit, expected 0-9 but got a",
);
}
#[test]
fn err_numeric_seconds_out_of_range() {
insta::assert_snapshot!(
Parser::new().parse_numeric(b"-05:30:60").unwrap_err(),
@"failed to parse seconds in UTC numeric offset: failed to parse seconds (requires a two digit integer): parameter 'time zone offset seconds' is not in the required range of -59..=59",
);
}
#[test]
fn err_numeric_fraction_non_empty() {
insta::assert_snapshot!(
Parser::new().parse_numeric(b"-05:30:44.").unwrap_err(),
@"failed to parse fractional seconds in UTC numeric offset: found decimal after seconds component, but did not find any digits after decimal",
);
insta::assert_snapshot!(
Parser::new().parse_numeric(b"-05:30:44,").unwrap_err(),
@"failed to parse fractional seconds in UTC numeric offset: found decimal after seconds component, but did not find any digits after decimal",
);
insta::assert_snapshot!(
Parser::new().parse_numeric(b"-05:30:44.a").unwrap_err(),
@"failed to parse fractional seconds in UTC numeric offset: found decimal after seconds component, but did not find any digits after decimal",
);
insta::assert_snapshot!(
Parser::new().parse_numeric(b"-05:30:44,a").unwrap_err(),
@"failed to parse fractional seconds in UTC numeric offset: found decimal after seconds component, but did not find any digits after decimal",
);
insta::assert_snapshot!(
Parser::new().parse_numeric(b"-053044.a").unwrap_err(),
@"failed to parse fractional seconds in UTC numeric offset: found decimal after seconds component, but did not find any digits after decimal",
);
insta::assert_snapshot!(
Parser::new().parse_numeric(b"-053044,a").unwrap_err(),
@"failed to parse fractional seconds in UTC numeric offset: found decimal after seconds component, but did not find any digits after decimal",
);
}
#[test]
fn err_numeric_subminute_disabled_but_desired() {
insta::assert_snapshot!(
Parser::new().subminute(false).parse_numeric(b"-05:59:32").unwrap_err(),
@"subminute precision for UTC numeric offset is not enabled in this context (must provide only integral minutes)",
);
}
#[test]
fn err_zulu_disabled_but_desired() {
insta::assert_snapshot!(
Parser::new().zulu(false).parse(b"Z").unwrap_err(),
@"found `Z` where a numeric UTC offset was expected (this context does not permit the Zulu offset)",
);
insta::assert_snapshot!(
Parser::new().zulu(false).parse(b"z").unwrap_err(),
@"found `z` where a numeric UTC offset was expected (this context does not permit the Zulu offset)",
);
}
#[test]
fn err_numeric_too_big_for_offset() {
let numeric = Numeric {
sign: b::Sign::Positive,
hours: b::OffsetHours::MAX,
minutes: Some(b::OffsetMinutes::MAX),
seconds: Some(b::OffsetSeconds::MAX),
nanoseconds: Some(499_999_999),
};
assert_eq!(numeric.to_offset().unwrap(), Offset::MAX);
let numeric = Numeric {
sign: b::Sign::Positive,
hours: b::OffsetHours::MAX,
minutes: Some(b::OffsetMinutes::MAX),
seconds: Some(b::OffsetSeconds::MAX),
nanoseconds: Some(500_000_000),
};
insta::assert_snapshot!(
numeric.to_offset().unwrap_err(),
@"due to precision loss from fractional seconds, time zone offset is rounded to a value that is out of bounds",
);
}
#[test]
fn err_numeric_too_small_for_offset() {
let numeric = Numeric {
sign: b::Sign::Negative,
hours: b::OffsetHours::MAX,
minutes: Some(b::OffsetMinutes::MAX),
seconds: Some(b::OffsetSeconds::MAX),
nanoseconds: Some(499_999_999),
};
assert_eq!(numeric.to_offset().unwrap(), Offset::MIN);
let numeric = Numeric {
sign: b::Sign::Negative,
hours: b::OffsetHours::MAX,
minutes: Some(b::OffsetMinutes::MAX),
seconds: Some(b::OffsetSeconds::MAX),
nanoseconds: Some(500_000_000),
};
insta::assert_snapshot!(
numeric.to_offset().unwrap_err(),
@"due to precision loss from fractional seconds, time zone offset is rounded to a value that is out of bounds",
);
}
}