Skip to content

Commit

Permalink
misc debug statements
Browse files Browse the repository at this point in the history
do not commit this

[ci skip]
  • Loading branch information
jtmoon79 committed Aug 23, 2022
1 parent b4c9a6f commit 0d0968f
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ fn test_datetime_from_str_with_spaces() {
assert!(Utc.datetime_from_str("\nAug 09 2013 23:54:35", "%b %d %Y %H:%M:%S\n").is_err());
assert!(Utc.datetime_from_str("Aug 09 2013 23:54:35 ", "%b %d %Y %H:%M:%S\n").is_err());
assert!(Utc.datetime_from_str("Aug 09 2013 23:54:35", "%b %d %Y\t%H:%M:%S").is_err());
assert!(Utc.datetime_from_str("Aug 09 2013 23:54:35 !!!", "%b %d %Y %H:%M:%S ").is_err());
}

#[test]
Expand Down
67 changes: 62 additions & 5 deletions src/format/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,28 +268,44 @@ where
}
}};
}
eprintln!("
->parse_internal(
parsed: {:?},
s: {:?},
...
)", parsed, s);

for item in items {
eprint!(" parse_internal item ");
match *item.borrow() {
Item::Literal(prefix) => {
eprintln!("match Item::Literal");
if s.len() < prefix.len() {
eprintln!(" ~~Err(TOO_SHORT, {:?})", s);
return Err((s, TOO_SHORT));
}
if !s.starts_with(prefix) {
eprintln!(" ~~Err(INVALID, {:?})", s);
return Err((s, INVALID));
}
s = &s[prefix.len()..];
eprintln!(" s {:?}", s);
}

#[cfg(any(feature = "alloc", feature = "std", test))]
Item::OwnedLiteral(ref prefix) => {
eprintln!("match Item::OwnedLiteral({:?})", prefix);
eprintln!(" s {:?}", s);
if s.len() < prefix.len() {
eprintln!(" ~~Err(TOO_SHORT, {:?})", s);
return Err((s, TOO_SHORT));
}
if !s.starts_with(&prefix[..]) {
eprintln!(" ~~Err(INVALID, {:?})", s);
return Err((s, INVALID));
}
s = &s[prefix.len()..];
eprintln!(" s {:?}", s);
}

Item::Space(space) => {
Expand All @@ -301,19 +317,22 @@ where
eprintln!("c {:?}", c);
if ! c.is_whitespace() {
// `s` whitespace must match each `item`
eprintln!("\n ~~Err(INVALID, {:?})", s);
eprintln!(" ~~Err(INVALID, {:?}) (Item::Space but s.next() {:?} is not whitespace)", s, c);
return Err((s, INVALID));
}
eprint!(" match space.chars().next() ");
// whitespace character must match
match &space.chars().next() {
Some(spacec) => {
eprintln!("c {:?}", c);
if c != spacec {
eprintln!("\n ~~Err(INVALID, {:?})", s);
eprintln!(" ~~Err(INVALID, {:?}) (mismatched whitespace)", s);
return Err((s, INVALID));
}
}
None => {
eprintln!("\n ~~Err(INVALID, {:?})", s);
eprintln!("None");
eprintln!(" ~~Err(INVALID, {:?})", s);
return Err((s, INVALID));
}
}
Expand All @@ -322,19 +341,23 @@ where
None => {
eprintln!("None");
// `s` whitespace must match each `items`
eprintln!("\n ~~Err(INVALID, {:?})", s);
eprintln!(" ~~Err(INVALID, {:?})", s);
return Err((s, INVALID));
}
}
eprintln!(" s {:?}", s);
}

#[cfg(any(feature = "alloc", feature = "std", test))]
Item::OwnedSpace(_) => {
Item::OwnedSpace(ref _s) => {
eprintln!("match Item::OwnedSpace({:?}", _s);
eprintln!(" s {:?}", s);
s = s.trim_left();
eprintln!(" s {:?}", s);
}

Item::Numeric(ref spec, ref _pad) => {
eprintln!("match Item::Numeric");
use super::Numeric::*;
type Setter = fn(&mut Parsed, i64) -> ParseResult<()>;

Expand Down Expand Up @@ -364,7 +387,9 @@ where
Internal(ref int) => match int._dummy {},
};

eprintln!(" width {:?}, signed {:?}, set ...", width, signed);
s = s.trim_left();
eprintln!(" s {:?}", s);
let v = if signed {
if s.starts_with('-') {
let v = try_consume!(scan::number(&s[1..], 1, usize::MAX));
Expand All @@ -382,30 +407,42 @@ where
}

Item::Fixed(ref spec) => {
eprintln!("match Item::Fixed");
use super::Fixed::*;

eprint!(" parse_internal spec ");
match spec {
&ShortMonthName => {
eprintln!("match ShortMonthName");
let month0 = try_consume!(scan::short_month0(s));
parsed.set_month(i64::from(month0) + 1).map_err(|e| (s, e))?;
eprintln!(" s {:?}", s);
}

&LongMonthName => {
eprintln!("match LongMonthName");
let month0 = try_consume!(scan::short_or_long_month0(s));
parsed.set_month(i64::from(month0) + 1).map_err(|e| (s, e))?;
eprintln!(" s {:?}", s);
}

&ShortWeekdayName => {
eprintln!("match ShortWeekdayName");
let weekday = try_consume!(scan::short_weekday(s));
parsed.set_weekday(weekday).map_err(|e| (s, e))?;
eprintln!(" s {:?}", s);
}

&LongWeekdayName => {
eprintln!("match LongWeekdayName");
let weekday = try_consume!(scan::short_or_long_weekday(s));
parsed.set_weekday(weekday).map_err(|e| (s, e))?;
eprintln!(" s {:?}", s);
}

&LowerAmPm | &UpperAmPm => {
eprintln!("match LowerAmPm|UpperAmPm");
eprintln!(" s {:?}", s);
if s.len() < 2 {
return Err((s, TOO_SHORT));
}
Expand All @@ -416,16 +453,19 @@ where
};
parsed.set_ampm(ampm).map_err(|e| (s, e))?;
s = &s[2..];
eprintln!(" s {:?}", s);
}

&Nanosecond | &Nanosecond3 | &Nanosecond6 | &Nanosecond9 => {
eprintln!("match Nanosecond|...");
if s.starts_with('.') {
let nano = try_consume!(scan::nanosecond(&s[1..]));
parsed.set_nanosecond(nano).map_err(|e| (s, e))?;
}
}

&Internal(InternalFixed { val: InternalInternal::Nanosecond3NoDot }) => {
eprintln!("match Internal(Nanosecond3NoDot)");
if s.len() < 3 {
return Err((s, TOO_SHORT));
}
Expand All @@ -434,51 +474,65 @@ where
}

&Internal(InternalFixed { val: InternalInternal::Nanosecond6NoDot }) => {
eprintln!("match Internal(Nanosecond6NoDot)");
if s.len() < 6 {
return Err((s, TOO_SHORT));
}
let nano = try_consume!(scan::nanosecond_fixed(s, 6));
parsed.set_nanosecond(nano).map_err(|e| (s, e))?;
eprintln!(" s {:?}", s);
}

&Internal(InternalFixed { val: InternalInternal::Nanosecond9NoDot }) => {
eprintln!("match Internal(Nanosecond9NoDot)");
if s.len() < 9 {
eprintln!(" ~~parse_internal Err(TOO_SHORT, {:?})", s);
return Err((s, TOO_SHORT));
}
let nano = try_consume!(scan::nanosecond_fixed(s, 9));
parsed.set_nanosecond(nano).map_err(|e| (s, e))?;
}

&TimezoneName => {
eprintln!("match TimezoneName");
try_consume!(scan::timezone_name_skip(s));
}

&TimezoneOffsetColon
| &TimezoneOffsetDoubleColon
| &TimezoneOffsetTripleColon
| &TimezoneOffset => {
eprintln!("match TimezoneOffsetColon|...");
eprintln!(" s {:?}", s);
let offset = try_consume!(scan::timezone_offset(
s.trim_left(),
scan::colon_or_space
));
parsed.set_offset(i64::from(offset)).map_err(|e| (s, e))?;
eprintln!(" s {:?}", s);
}

&TimezoneOffsetColonZ | &TimezoneOffsetZ => {
eprintln!("match TimezoneOffsetColonZ|TimezoneOffsetZ");
eprintln!(" s {:?}", s);
let offset = try_consume!(scan::timezone_offset_zulu(
s.trim_left(),
scan::colon_or_space
));
parsed.set_offset(i64::from(offset)).map_err(|e| (s, e))?;
eprintln!(" s {:?}", s);
}
&Internal(InternalFixed {
val: InternalInternal::TimezoneOffsetPermissive,
}) => {
eprintln!("match InternalFixed");
eprintln!(" s {:?}", s);
let offset = try_consume!(scan::timezone_offset_permissive(
s.trim_left(),
scan::colon_or_space
));
parsed.set_offset(i64::from(offset)).map_err(|e| (s, e))?;
eprintln!(" s {:?}", s);
}

&RFC2822 => try_consume!(parse_rfc2822(parsed, s)),
Expand All @@ -487,15 +541,18 @@ where
}

Item::Error => {
eprintln!(" ~~parse_internal Err(BAD_FORMAT, {:?})", s);
return Err((s, BAD_FORMAT));
}
}
}

// if there are trailling chars, it is an error
if !s.is_empty() {
eprintln!(" ~~parse_internal Err(TOO_LONG, {:?})", s);
Err((s, TOO_LONG))
} else {
eprintln!(" ~~parse_internal Ok({:?})", s);
Ok(s)
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/format/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,9 +649,11 @@ impl Parsed {
/// If parsed fields include an UTC offset, it also has to be consistent to
/// [`offset`](#structfield.offset).
pub fn to_datetime_with_timezone<Tz: TimeZone>(&self, tz: &Tz) -> ParseResult<DateTime<Tz>> {
eprintln!("->to_datetime_with_timezone(tz)");
// if we have `timestamp` specified, guess an offset from that.
let mut guessed_offset = 0;
if let Some(timestamp) = self.timestamp {
eprintln!(" to_datetime_with_timezone: timestamp = {:?}", self.timestamp);
// make a naive `DateTime` from given timestamp and (if any) nanosecond.
// an empty `nanosecond` is always equal to zero, so missing nanosecond is fine.
let nanosecond = self.nanosecond.unwrap_or(0);
Expand All @@ -662,6 +664,7 @@ impl Parsed {

// checks if the given `DateTime` has a consistent `Offset` with given `self.offset`.
let check_offset = |dt: &DateTime<Tz>| {
eprintln!(" to_datetime_with_timezone: check_offset = {:?}", self.offset);
if let Some(offset) = self.offset {
dt.offset().fix().local_minus_utc() == offset
} else {
Expand All @@ -672,16 +675,24 @@ impl Parsed {
// `guessed_offset` should be correct when `self.timestamp` is given.
// it will be 0 otherwise, but this is fine as the algorithm ignores offset for that case.
let datetime = self.to_naive_datetime_with_offset(guessed_offset)?;
eprintln!(" to_datetime_with_timezone: datetime {:?}", datetime);
match tz.from_local_datetime(&datetime) {
LocalResult::None => Err(IMPOSSIBLE),
LocalResult::None => {
eprintln!("~~to_datetime_with_timezone: LocalResult::None return Err(IMPOSSIBLE)");
Err(IMPOSSIBLE)
}
LocalResult::Single(t) => {
eprintln!(" to_datetime_with_timezone: LocalResult::Single({:?})", t);
if check_offset(&t) {
eprintln!("~~to_datetime_with_timezone: Ok({:?})", t);
Ok(t)
} else {
eprintln!("~~to_datetime_with_timezone: Err(IMPOSSIBLE)");
Err(IMPOSSIBLE)
}
}
LocalResult::Ambiguous(min, max) => {
eprintln!("~~to_datetime_with_timezone: LocalResult::Ambiguous({:?}, {:?})", min, max);
// try to disambiguate two possible local dates by offset.
match (check_offset(&min), check_offset(&max)) {
(false, false) => Err(IMPOSSIBLE),
Expand Down
16 changes: 13 additions & 3 deletions src/format/strftime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ impl<'a> Iterator for StrftimeItems<'a> {
type Item = Item<'a>;

fn next(&mut self) -> Option<Item<'a>> {
eprintln!(" ->StrftimeItems::Iterator::next()");
// we have some reconstructed items to return
if !self.recons.is_empty() {
let item;
Expand All @@ -281,12 +282,18 @@ impl<'a> Iterator for StrftimeItems<'a> {
return Some(item);
}

eprint!(" match self.remainder.chars().next() ");

match self.remainder.chars().next() {
// we are done
None => None,
None => {
eprintln!("None");
None
},

// the next item is a specifier
Some('%') => {
eprintln!("%");
self.remainder = &self.remainder[1..];

macro_rules! next {
Expand Down Expand Up @@ -467,7 +474,7 @@ impl<'a> Iterator for StrftimeItems<'a> {

// the next item is space
Some(c) if c.is_whitespace() => {
eprintln!("match c.is_whitespace()");
eprintln!("c.is_whitespace()");
// `%` is not a whitespace, so `c != '%'` is redundant
let mut nextspec = self
.remainder
Expand All @@ -486,14 +493,17 @@ impl<'a> Iterator for StrftimeItems<'a> {
}

// the next item is literal
_ => {
_l => {
eprintln!("literal {:?}", _l);
let nextspec = self
.remainder
.find(|c: char| c.is_whitespace() || c == '%')
.unwrap_or(self.remainder.len());
assert!(nextspec > 0);
let item = lit!(&self.remainder[..nextspec]);
eprintln!(" remainder {:?}", self.remainder);
self.remainder = &self.remainder[nextspec..];
eprintln!(" remainder {:?}", self.remainder);
Some(item)
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/naive/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,28 @@ fn test_datetime_timestamp() {
fn test_datetime_from_str() {
// valid cases
let valid = [
"2015-2-18T23:16:9.15",
"-77-02-18T23:16:09",
" +82701 - 05 - 6 T 15 : 9 : 60.898989898989 ",
"2015-02-18T23:16:09.153",
//"2015-2-18T23:16:9.15",
//"-77-02-18T23:16:09",
//" +82701 - 05 - 6 T 15 : 9 : 60.898989898989 ",
];
for &s in &valid {
eprintln!(" {:?}.parse::<NaiveDateTime>", s);
let d = match s.parse::<NaiveDateTime>() {
Ok(d) => d,
Err(e) => panic!("parsing `{}` has failed: {}", s, e),
};
eprintln!(" d {:?}", d);
let s_ = format!("{:?}", d);
// `s` and `s_` may differ, but `s.parse()` and `s_.parse()` must be same
eprintln!(" {:?}.parse::<NaiveDateTime>", s_);
let d_ = match s_.parse::<NaiveDateTime>() {
Ok(d) => d,
Err(e) => {
panic!("`{}` is parsed into `{:?}`, but reparsing that has failed: {}", s, d, e)
}
};
eprintln!(" d_ {:?}", d_);
assert!(
d == d_,
"`{}` is parsed into `{:?}`, but reparsed result \
Expand Down
Loading

0 comments on commit 0d0968f

Please sign in to comment.