Skip to content

Commit

Permalink
Correctly parse ISO 8601 offset
Browse files Browse the repository at this point in the history
Previously, the minute was effectively required. This goes against the
specification.
  • Loading branch information
jhpratt committed Jul 8, 2023
1 parent 52e5cee commit c435649
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions tests/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ fn iso_8601() {
OffsetDateTime::parse("20210102T0304Z", &Iso8601::DEFAULT),
Ok(datetime!(2021-01-02 03:04:00 UTC))
);
assert_eq!(UtcOffset::parse("+07", &Iso8601::DEFAULT), Ok(offset!(+7)));
assert_eq!(
UtcOffset::parse("+0304", &Iso8601::DEFAULT),
Ok(offset!(+03:04))
Expand Down
20 changes: 13 additions & 7 deletions time/src/parsing/iso8601.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,23 @@ impl<const CONFIG: EncodedConfig> Iso8601<CONFIG> {
};
}

let input = min(input)
.and_then(|parsed_item| {
parsed_item.consume_value(|min| {
parsed.set_offset_minute_signed(if sign == b'-' {
match min(input) {
Some(ParsedItem(new_input, min)) => {
input = new_input;
parsed
.set_offset_minute_signed(if sign == b'-' {
-(min as i8)
} else {
min as _
})
})
})
.ok_or(InvalidComponent("offset minute"))?;
.ok_or(InvalidComponent("offset minute"))?;
}
None => {
// Omitted offset minute is assumed to be zero.
parsed.set_offset_minute_signed(0);
}
}

// If `:` was present, the format has already been set to extended. As such, this call
// will do nothing in that case. If there wasn't `:` but minutes were
// present, we know it's the basic format. Do not use `?` on the call, as
Expand Down

0 comments on commit c435649

Please sign in to comment.