question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid datetime can produce partial parse. Should return None instead

See original GitHub issue

I came across this silent failure when parsing a third party datetime with an unusual separator:

>>> import ciso8601

>>> ciso8601.parse_datetime('2017-05-29 17:36:00Z')
datetime.datetime(2017, 5, 29, 17, 36, tzinfo=<UTC>)

#Now for a completely invalid string
>>> print(ciso8601.parse_datetime('Completely invalid!'))
None

#Now for an out of spec string (invalid separator)
>>> ciso8601.parse_datetime('2017-05-29_17:36:00Z')
datetime.datetime(2017, 5, 29, 0, 0)

It seems that the parser sees a character that it doesn’t recognize and stops parsing?

I would have expected that if the entire string cannot be parsed, it would return None as parse_datetime does for other parse failures, not silently return a datetime without time or tzinfo.

This lead to a sneaky bug in the code.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
movermeyercommented, Sep 1, 2017

I’m not sure what sort of PR you’d be expecting. If it were my decision (ie. my library), I think I would change it to return None for all invalid input.

If that’s what you’d like to see, I could take a shot at that. But it would be a change of behaviour, so anyone who has written code that relies on this behaviour would need to change their code.

As an example of usage that would change, this test would change to expect None

1reaction
thomasstcommented, May 9, 2018

Thanks for this. Having both parse_datetime and parse_datetime_as_naive as you described sounds good to me!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issue 30681: email.utils.parsedate_to_datetime() should ...
When _parsedate_tz() fails to parse the date and returns None: >>> from email.utils import parsedate_to_datetime >>> parsedate_to_datetime('0') ...
Read more >
Parsing a date in python without using a default - Stack Overflow
That only solves the empty string case. When I have a partial date, it is still defaulting the fields not specified, but gets...
Read more >
DateParser Documentation - Read the Docs
Returns Returns datetime representing parsed date if successful, else returns None. Return type datetime. Raises ValueError: Unknown Language, ...
Read more >
Date in chrono - Rust
You almost certainly want to be using a NaiveDate instead of this type. ... Returns None on invalid hour, minute, second and/or millisecond....
Read more >
NaiveDate in chrono::naive - Rust - Docs.rs
Returns None on the out-of-range date, invalid month and/or day. ... All parsed fields should be consistent to each other, otherwise it's an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found