Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can parse invalid dates #50

Closed
belt opened this issue Jun 27, 2011 · 2 comments
Closed

can parse invalid dates #50

belt opened this issue Jun 27, 2011 · 2 comments

Comments

@belt
Copy link

belt commented Jun 27, 2011

One might expect Chronic.parse('30 February 2011') to yield nil... as there never a 30'th of February... yet it yields 02 March 2011.

30 days hath September, April, June and November

Same-ish goes for the "31st" of those 4 months too yielding the 1st of the following month.

Same-ish goes for Chronic.parse('31 February 2011') yielding 03 March 2011... and the 29th of February yielding the 1st of March on non-leap years.

@leejarvis
Copy link
Collaborator

This is actually because of how the Ruby Time class deals with day overflows. An example:

>> Time.local(2011, 2, 31)
=> 2011-03-03 00:00:00 +0000
>> Chronic.parse('31 feb 2011') == Time.local(2011, 2, 31, 12)
=> true

The Time class raises an ArgumentError when a day is past 31 (of course it's not possible to have a day later than 31 in any month), but if it's later than the last day of said month, the month will be incremented and days will be added from the start of this month. Chronic has always relied on this functionality be it a feature or a hinderance.

The internals of Chronic don't actually care what 31 is (in certain sense). It's relative information. In this case 31 represents 31 days from the beginning of February, which of course, if you count it, is 03/03/2011 (unless it's a leap year).

I'll have a think about this and leave this issue open for discussion, because I do agree with you. Libraries shouldn't rely on this functionality but it's how Chronic has always done it, so there's always that one exception to the rule.

@leejarvis
Copy link
Collaborator

Summoning @mojombo on this discussion. We can go one of 3 ways:

  • Return nil when an out of month day is supplied
  • Raise when an out of month day is supplied
  • Add a :strict option, disabled by default, which will raise when an out of month day is supplied

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants