Invalid datetime can produce partial parse. Should return None instead
See original GitHub issueI 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:
- Created 6 years ago
- Reactions:1
- Comments:14 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
Thanks for this. Having both
parse_datetime
andparse_datetime_as_naive
as you described sounds good to me!