delorean icon indicating copy to clipboard operation
delorean copied to clipboard

Error parsing when current day is not in month

Open hector opened this issue 8 years ago • 10 comments

Hi,

Since delorean.parse() fills missing info from the string with the current date, it will fail if trying to parse a date with month if your current day is out of that month. An example to make it clear:

  1. Today is 30th September
  2. I do `delorean.parse('2000-02')``
  3. I will get "Unknown string format" since 30th does not exist for February

What would be the correct approach to parse this kind of strings? Is there a way to parse without filling the missing information with the current date (for example with day 1, hour 0 etc)?

hector avatar Sep 30 '15 10:09 hector

I wasn't aware of this behaviour and something I will have to look into the usage of dateutil.

myusuf3 avatar Oct 01 '15 18:10 myusuf3

this is related also to this bug i think? https://github.com/myusuf3/delorean/issues/99

gordol avatar Jun 30 '17 05:06 gordol

@gordol was this related, can this be closed?

myusuf3 avatar Jun 30 '17 16:06 myusuf3

It's a dateutil issue upstream. Not fixed and they aren't planning to fix any time soon it seems.

gordol avatar Jun 30 '17 16:06 gordol

@gordol did they give a reason?

myusuf3 avatar Jun 30 '17 16:06 myusuf3

i think this is actually a different bug...

https://github.com/dateutil/dateutil/issues/149#issuecomment-186367034

that is the reason given. basically, it implements an old RFC.

gordol avatar Jul 01 '17 09:07 gordol

delorean basically just implements dateutil.parser.parse

This module attempts to be forgiving with regards to unlikely input formats, returning a datetime object even for dates which are ambiguous. If an element of a date/time stamp is omitted, the following rules are applied: - If AM or PM is left unspecified, a 24-hour clock is assumed, however, an hour

on a 12-hour clock (0 <= hour <= 12) must be specified if AM or PM is specified. If a time zone is omitted, a timezone-naive datetime is returned. If any other elements are missing, they are taken from the datetime.datetime object passed to the parameter default. If this results in a day number exceeding the valid number of days per month, the value falls back to the end of the month.

Anyway, this led me down a journey into dateutil... and it seems this may have been fixed in 2.5?

https://github.com/dateutil/dateutil/issues/25

currently delorean lists dateutil 2.4.2 as the required version.

however, i think there are some other issues in dateutil after 2.5.1...

so you may want to stick with 2.5.1 for now.

see the changelog here where this was fixed in 2.5.0: https://github.com/dateutil/dateutil/blob/38232aacc3c46ef3b649ec730a2444ed13fe3894/NEWS#L150

gordol avatar Jul 01 '17 10:07 gordol

2.6 has a new parsing overhaul, so maybe give it a whirl?

gordol avatar Jul 01 '17 10:07 gordol

https://github.com/myusuf3/delorean/issues/87

gordol avatar Jul 01 '17 10:07 gordol

you can pass a default date to the parse() method also... to work around this. or you can simply set the first day of your date string to "1" to be first day of month. by default it uses the current day's date.

gordol avatar Jul 01 '17 10:07 gordol