myusuf3 / delorean

Delorean: Time Travel Made Easy
http://delorean.rtfd.org/
MIT License
1.84k stars 128 forks source link

Error parsing when current day is not in month #74

Open hector opened 9 years ago

hector commented 9 years ago

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)?

myusuf3 commented 9 years ago

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

gordol commented 7 years ago

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

myusuf3 commented 7 years ago

@gordol was this related, can this be closed?

gordol commented 7 years ago

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

myusuf3 commented 7 years ago

@gordol did they give a reason?

gordol commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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

gordol commented 7 years ago

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

gordol commented 7 years ago

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.