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 May 24, 2023
1 parent d163b7e commit 36899a1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
40 changes: 19 additions & 21 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,31 +1058,29 @@ fn test_datetime_parse_from_str() {
//
// %::z
//
assert_eq!(parse("Aug 09 2013 23:54:35 -0900", "%b %d %Y %H:%M:%S %::z"), Ok(dt));
assert_eq!(parse("Aug 09 2013 23:54:35 -09:00", "%b %d %Y %H:%M:%S %::z"), Ok(dt));
assert!(parse("Aug 09 2013 23:54:35 -09 : 00", "%b %d %Y %H:%M:%S %::z").is_err());
assert_eq!(parse("Aug 09 2013 23:54:35 -09:00:00", "%b %d %Y %H:%M:%S %::z"), Ok(dt));
assert_eq!(parse("Aug 09 2013 23:54:35 -09:00:00 ", "%b %d %Y %H:%M:%S %::z "), Ok(dt));
// mid-string
assert_eq!(parse("Aug 09 2013 -09:00:00 23:54:35", "%b %d %Y %::z %H:%M:%S"), Ok(dt));
// surrounded with colons
assert_eq!(parse("Aug 09 2013 23:54:35 :-09:00:00: ", "%b %d %Y %H:%M:%S :%::z: "), Ok(dt));
// mismatching colon expectations
assert!(parse("Aug 09 2013 23:54:35 -09:00:00", "%b %d %Y %H:%M:%S %::z").is_err());
assert!(parse("Aug 09 2013 23:54:35 -:09:00", "%b %d %Y %H:%M:%S %::z").is_err());
assert!(parse("Aug 09 2013 23:54:35 -09:00:", "%b %d %Y %H:%M:%S %::z").is_err());
assert!(parse("Aug 09 2013 23:54:35 -09 : 00 : 00", "%b %d %Y %H:%M:%S %::z").is_err());
assert!(parse("Aug 09 2013 23:54:35 -09::00", "%b %d %Y %H:%M:%S %::z").is_err());
assert!(parse("Aug 09 2013 23:54:35 -09::00", "%b %d %Y %H:%M:%S %:z").is_err());
// mismatching optional colons
assert!(parse("Aug 09 2013 23:54:35 -090000", "%b %d %Y %H:%M:%S %::z").is_err());
assert!(parse("Aug 09 2013 23:54:35 -09:0000", "%b %d %Y %H:%M:%S %::z").is_err());
assert!(parse("Aug 09 2013 23:54:35 -0900:00", "%b %d %Y %H:%M:%S %::z").is_err());
// wrong timezone data
assert!(parse("Aug 09 2013 23:54:35 -09", "%b %d %Y %H:%M:%S %::z").is_err());
assert_eq!(parse("Aug 09 2013 23:54:35 -09001234", "%b %d %Y %H:%M:%S %::z1234"), Ok(dt));
assert_eq!(parse("Aug 09 2013 23:54:35 -09:001234", "%b %d %Y %H:%M:%S %::z1234"), Ok(dt));
assert_eq!(parse("Aug 09 2013 23:54:35 -0900 ", "%b %d %Y %H:%M:%S %::z "), Ok(dt));
assert_eq!(parse("Aug 09 2013 23:54:35 -0900\t\n", "%b %d %Y %H:%M:%S %::z\t\n"), Ok(dt));
assert_eq!(parse("Aug 09 2013 23:54:35 -0900:", "%b %d %Y %H:%M:%S %::z:"), Ok(dt));
assert_eq!(parse("Aug 09 2013 23:54:35 :-0900:0", "%b %d %Y %H:%M:%S :%::z:0"), Ok(dt));
// mismatching colons and spaces
assert!(parse("Aug 09 2013 23:54:35 :-0900: ", "%b %d %Y %H:%M:%S :%::z::").is_err());
// mismatching colons expectations
assert!(parse("Aug 09 2013 23:54:35 -09:00:00", "%b %d %Y %H:%M:%S %::z").is_err());
assert_eq!(parse("Aug 09 2013 -0900: 23:54:35", "%b %d %Y %::z: %H:%M:%S"), Ok(dt));
assert_eq!(parse("Aug 09 2013 :-0900:0 23:54:35", "%b %d %Y :%::z:0 %H:%M:%S"), Ok(dt));
// mismatching colons expectations mid-string
assert!(parse("Aug 09 2013 :-0900: 23:54:35", "%b %d %Y :%::z %H:%M:%S").is_err());
// mismatching colons expectations, before end
assert!(parse("Aug 09 2013 23:54:35 -09:00:00 ", "%b %d %Y %H:%M:%S %::z ").is_err());
assert!(parse("Aug 09 2013 23:54:35 -09001234", "%b %d %Y %H:%M:%S %::z1234").is_err());
assert!(parse("Aug 09 2013 23:54:35 -09:001234", "%b %d %Y %H:%M:%S %::z1234").is_err());
assert!(parse("Aug 09 2013 23:54:35 -0900 ", "%b %d %Y %H:%M:%S %::z ").is_err());
assert!(parse("Aug 09 2013 23:54:35 -0900\t\n", "%b %d %Y %H:%M:%S %::z\t\n").is_err());
assert!(parse("Aug 09 2013 23:54:35 -0900:", "%b %d %Y %H:%M:%S %::z:").is_err());
assert!(parse("Aug 09 2013 23:54:35 :-0900:0", "%b %d %Y %H:%M:%S :%::z:0").is_err());

//
// %:::z
Expand Down
1 change: 0 additions & 1 deletion src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ pub enum Fixed {
TimezoneOffsetColon,
/// Offset from the local time to UTC with seconds (`+09:00:00` or `-04:00:00` or `+00:00:00`).
///
/// In the parser, the colon may be omitted,
/// The offset is limited from `-24:00:00` to `+24:00:00`,
/// which is the same as [`FixedOffset`](../offset/struct.FixedOffset.html)'s range.
TimezoneOffsetDoubleColon,
Expand Down
13 changes: 12 additions & 1 deletion src/format/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,6 @@ where
}

&TimezoneOffsetColon
| &TimezoneOffsetDoubleColon
| &TimezoneOffset
| &TimezoneOffsetColonZ
| &TimezoneOffsetZ => {
Expand All @@ -505,6 +504,18 @@ where
parsed.set_offset(i64::from(offset)).map_err(|e| (s, e))?;
}

&TimezoneOffsetDoubleColon => {
s = scan::trim1(s);
let offset_format = UtcOffsetFormat {
precision: OffsetPrecision::Seconds,
colons: Colon::Colon,
allow_zulu: false,
padding: Pad::Zero,
};
let offset = try_consume!(scan::utc_offset(s, offset_format));
parsed.set_offset(i64::from(offset)).map_err(|e| (s, e))?;
}

&TimezoneOffsetTripleColon => {
s = scan::trim1(s);
let offset_format = UtcOffsetFormat {
Expand Down

0 comments on commit 36899a1

Please sign in to comment.