Skip to content

Instantly share code, notes, and snippets.

@moiristo
Created July 29, 2010 09:57
Show Gist options
  • Save moiristo/497753 to your computer and use it in GitHub Desktop.
Save moiristo/497753 to your computer and use it in GitHub Desktop.
def gregorian_date?(date)
return false if date.nil?
(date.month == 2 and date.day == 29) ? Date.gregorian_leap?(date.year) : true
end
@moiristo
Copy link
Author

This method only returns false when date is nil or when the date is a 'leap date' (so, the 29th of february), while the year is not a gregorian leap year. Imho, there is currently no easy way to do this in ruby, all validation methods in the Date class will say that for example february 29th, 1500 is a valid date, while it isn't according to the gregorian calendar. Used this to solve the PostgreSQL error 'PGError: ERROR: date/time field value out of range', which is thrown when the database server detects an invalid date is passed in a query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment