Skip to content

Commit

Permalink
be exact about whitespace
Browse files Browse the repository at this point in the history
Be exact about whitespace in parsing. This drastically changes
pattern matching in `format::parse::parse`.

Be more exacting about colons and whitespace around timezones.

Issue chronotope#660
  • Loading branch information
jtmoon79 committed Aug 27, 2022
1 parent 0d0968f commit b3b1954
Show file tree
Hide file tree
Showing 14 changed files with 620 additions and 217 deletions.
2 changes: 2 additions & 0 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::offset::{TimeZone, Utc};
use crate::oldtime::Duration as OldDuration;
use crate::DateTime;
use crate::{Datelike, Weekday};
use crate::dpl;

/// ISO 8601 calendar date with time zone.
///
Expand Down Expand Up @@ -337,6 +338,7 @@ where
#[cfg(any(feature = "alloc", feature = "std", test))]
#[inline]
pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>> {
dpl!("{:?}.format({:?})", self, fmt);
self.format_with_items(StrftimeItems::new(fmt))
}

Expand Down
12 changes: 11 additions & 1 deletion src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::offset::{FixedOffset, TimeZone, Utc};
use crate::oldtime::Duration;
#[cfg(feature = "clock")]
use crate::Datelike;
use crate::dpl;

#[test]
fn test_datetime_offset() {
Expand Down Expand Up @@ -165,41 +166,50 @@ fn test_rfc3339_opts_nonexhaustive() {

#[test]
fn test_datetime_from_str() {
dpl!("test_datetime_from_str: {:?}", line!());
assert_eq!(
"2015-02-18T23:16:9.15Z".parse::<DateTime<FixedOffset>>(),
Ok(FixedOffset::east(0).ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150))
);
dpl!("test_datetime_from_str: {:?}", line!());
assert_eq!(
"2015-02-18T23:16:9.15Z".parse::<DateTime<Utc>>(),
Ok(Utc.ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150))
);
dpl!("test_datetime_from_str: {:?}", line!());
assert_eq!(
"2015-02-18T23:16:9.15 UTC".parse::<DateTime<Utc>>(),
Ok(Utc.ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150))
);
dpl!("test_datetime_from_str: {:?}", line!());
assert_eq!(
"2015-02-18T23:16:9.15UTC".parse::<DateTime<Utc>>(),
Ok(Utc.ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150))
);

dpl!("test_datetime_from_str: {:?}", line!());
assert_eq!(
"2015-2-18T23:16:9.15Z".parse::<DateTime<FixedOffset>>(),
Ok(FixedOffset::east(0).ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150))
);
dpl!("test_datetime_from_str: {:?}", line!());
assert_eq!(
"2015-2-18T13:16:9.15-10:00".parse::<DateTime<FixedOffset>>(),
Ok(FixedOffset::west(10 * 3600).ymd(2015, 2, 18).and_hms_milli(13, 16, 9, 150))
);
dpl!("test_datetime_from_str: {:?}", line!());
assert!("2015-2-18T23:16:9.15".parse::<DateTime<FixedOffset>>().is_err());

dpl!("test_datetime_from_str: {:?}", line!());
assert_eq!(
"2015-2-18T23:16:9.15Z".parse::<DateTime<Utc>>(),
Ok(Utc.ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150))
);
dpl!("test_datetime_from_str: {:?}", line!());
assert_eq!(
"2015-2-18T13:16:9.15-10:00".parse::<DateTime<Utc>>(),
Ok(Utc.ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150))
);
dpl!("test_datetime_from_str: {:?}", line!());
assert!("2015-2-18T23:16:9.15".parse::<DateTime<Utc>>().is_err());

// no test for `DateTime<Local>`, we cannot verify that much.
Expand Down
14 changes: 7 additions & 7 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,27 +218,27 @@ pub enum Fixed {
///
/// It does not support parsing, its use in the parser is an immediate failure.
TimezoneName,
/// Offset from the local time to UTC (`+09:00` or `-04:00` or `+00:00`).
/// Offset from the local time to UTC (`+09:00` or `-0400` or `+00:00`).
///
/// In the parser, the colon can be omitted and/or surrounded with any amount of whitespace.
/// In the parser, the colon may be omitted,
/// The offset is limited from `-24:00` to `+24:00`,
/// which is the same as [`FixedOffset`](../offset/struct.FixedOffset.html)'s range.
TimezoneOffsetColon,
/// Offset from the local time to UTC with seconds (`+09:00:00` or `-04:00:00` or `+00:00:00`).
/// Offset from the local time to UTC with seconds (`+09:00:00` or `-0400:00` or `+000000`).
///
/// In the parser, the colon can be omitted and/or surrounded with any amount of whitespace.
/// 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,
/// Offset from the local time to UTC without minutes (`+09` or `-04` or `+00`).
///
/// In the parser, the colon can be omitted and/or surrounded with any amount of whitespace.
/// In the parser, the colon may be omitted,
/// The offset is limited from `-24` to `+24`,
/// which is the same as [`FixedOffset`](../offset/struct.FixedOffset.html)'s range.
TimezoneOffsetTripleColon,
/// Offset from the local time to UTC (`+09:00` or `-04:00` or `Z`).
/// Offset from the local time to UTC (`+09:00` or `-0400` or `Z`).
///
/// In the parser, the colon can be omitted and/or surrounded with any amount of whitespace,
/// In the parser, the colon may be omitted,
/// and `Z` can be either in upper case or in lower case.
/// The offset is limited from `-24:00` to `+24:00`,
/// which is the same as [`FixedOffset`](../offset/struct.FixedOffset.html)'s range.
Expand Down
Loading

0 comments on commit b3b1954

Please sign in to comment.