Skip to content

Commit

Permalink
Make parsing %:z conform to documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Jul 7, 2023
1 parent 420e068 commit ecb572c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ pub enum Fixed {
TimezoneName,
/// Offset from the local time to UTC (`+09:00` or `-04:00` or `+00:00`).
///
/// In the parser, the colon can be omitted and/or surrounded with any amount of whitespace.
/// The offset is limited from `-24:00` to `+24:00`,
/// which is the same as [`FixedOffset`](../offset/struct.FixedOffset.html)'s range.
TimezoneOffsetColon,
Expand Down
42 changes: 23 additions & 19 deletions src/format/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,14 +525,22 @@ where
try_consume!(Ok((s.trim_start_matches(|c: char| !c.is_whitespace()), ())));
}

&TimezoneOffsetColon
| &TimezoneOffset
| &TimezoneOffsetColonZ
| &TimezoneOffsetZ => {
&TimezoneOffset | &TimezoneOffsetZ => {
let offset_format = OffsetFormat {
precision: OffsetPrecision::Minutes,
colons: Colons::Maybe,
allow_zulu: spec == &TimezoneOffsetColonZ || spec == &TimezoneOffsetZ,
allow_zulu: spec == &TimezoneOffsetZ,
padding: Pad::Zero,
};
let offset = try_consume!(scan::utc_offset(s.trim_start(), offset_format));
parsed.set_offset(i64::from(offset)).map_err(|e| (s, e))?;
}

&TimezoneOffsetColon | &TimezoneOffsetColonZ => {
let offset_format = OffsetFormat {
precision: OffsetPrecision::Minutes,
colons: Colons::Colon,
allow_zulu: spec == &TimezoneOffsetColonZ,
padding: Pad::Zero,
};
let offset = try_consume!(scan::utc_offset(s.trim_start(), offset_format));
Expand Down Expand Up @@ -1164,14 +1172,14 @@ mod tests {
check("12345678", &[fixed(TimezoneOffsetColon)], Err(INVALID));
check("+1", &[fixed(TimezoneOffsetColon)], Err(TOO_SHORT));
check("+12", &[fixed(TimezoneOffsetColon)], Err(TOO_SHORT));
check("+123", &[fixed(TimezoneOffsetColon)], Err(TOO_SHORT));
check("+1234", &[fixed(TimezoneOffsetColon)], parsed!(offset: 45_240));
check("-1234", &[fixed(TimezoneOffsetColon)], parsed!(offset: -45_240));
check("−1234", &[fixed(TimezoneOffsetColon)], parsed!(offset: -45_240)); // MINUS SIGN (U+2212)
check("+12345", &[fixed(TimezoneOffsetColon)], Err(TOO_LONG));
check("+123456", &[fixed(TimezoneOffsetColon)], Err(TOO_LONG));
check("+1234567", &[fixed(TimezoneOffsetColon)], Err(TOO_LONG));
check("+12345678", &[fixed(TimezoneOffsetColon)], Err(TOO_LONG));
check("+123", &[fixed(TimezoneOffsetColon)], Err(INVALID));
check("+1234", &[fixed(TimezoneOffsetColon)], Err(INVALID));
check("-1234", &[fixed(TimezoneOffsetColon)], Err(INVALID));
check("−1234", &[fixed(TimezoneOffsetColon)], Err(INVALID)); // MINUS SIGN (U+2212)
check("+12345", &[fixed(TimezoneOffsetColon)], Err(INVALID));
check("+123456", &[fixed(TimezoneOffsetColon)], Err(INVALID));
check("+1234567", &[fixed(TimezoneOffsetColon)], Err(INVALID));
check("+12345678", &[fixed(TimezoneOffsetColon)], Err(INVALID));
check("1:", &[fixed(TimezoneOffsetColon)], Err(INVALID));
check("12:", &[fixed(TimezoneOffsetColon)], Err(INVALID));
check("12:3", &[fixed(TimezoneOffsetColon)], Err(INVALID));
Expand All @@ -1192,7 +1200,7 @@ mod tests {
check("+12:34:56:7", &[fixed(TimezoneOffsetColon)], Err(TOO_LONG));
check("+12:34:56:78", &[fixed(TimezoneOffsetColon)], Err(TOO_LONG));
check("+12:3456", &[fixed(TimezoneOffsetColon)], Err(TOO_LONG));
check("+1234:56", &[fixed(TimezoneOffsetColon)], Err(TOO_LONG));
check("+1234:56", &[fixed(TimezoneOffsetColon)], Err(INVALID));
check("−12:34", &[fixed(TimezoneOffsetColon)], parsed!(offset: -45_240)); // MINUS SIGN (U+2212)
check("−12 : 34", &[fixed(TimezoneOffsetColon)], Err(INVALID)); // MINUS SIGN (U+2212)
check("+12 :34", &[fixed(TimezoneOffsetColon)], Err(INVALID));
Expand Down Expand Up @@ -1221,11 +1229,7 @@ mod tests {
check("", &[fixed(TimezoneOffsetColon)], Err(TOO_SHORT));
check("+", &[fixed(TimezoneOffsetColon)], Err(TOO_SHORT));
check(":", &[fixed(TimezoneOffsetColon)], Err(INVALID));
check(
"+12345",
&[fixed(TimezoneOffsetColon), num(Numeric::Day)],
parsed!(offset: 45_240, day: 5),
);
check("+12345", &[fixed(TimezoneOffsetColon), num(Numeric::Day)], Err(INVALID));
check(
"+12:345",
&[fixed(TimezoneOffsetColon), num(Numeric::Day)],
Expand Down

0 comments on commit ecb572c

Please sign in to comment.