rails/rails — Issue #5222: DateTimeSelector builds invalid dates on leap day when discarding both day and month
Author: davedoan
Created: 2012-02-29 19:15:10+00:00
State: open

From ActionView::Helpers::DateTimeSelector:

@@@
# If the day is hidden and the month is visible, the day should be set to the 1st so all month choices are
# valid (otherwise it could be 31 and february wouldn't be a valid date)
if @datetime && @options[:discard_day] && !@options[:discard_month]
@datetime = @datetime.change(:day => 1)
end
@@@

On leap day, when :discard_month and :discard_day and are both enabled, this date reset code isn't run and the month and day remain set to 2/29. If the user then selects a year that is not a leap year (e.g. 2011), the date generated is invalid (2/29/2011 does not exist).

It also seems strange and inconsistent that the day would only be reset to 1 when we're not discarding the month. Perhaps it should always be reset regardless of the :discard_month option?