Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Improved "treat 0000-00-00 00:00:00 as nil Date" #1097

merged 1 commit into from

3 participants


It is not right to assume that if year is 0 then date is nil.
In our example we store dates in BC and 0000-01-01 is a valid date

So I've added check that for date to be nil year month and mday have to be 0.

I couldnt find a test for this case and wasnt sure where is a good place to add it.

Kirill R


'"Year zero" does not exist in the widely used Gregorian calendar or in its predecessor, the Julian calendar. Under those systems, the year 1 BC is followed by AD 1.'

Are you sure that 0000-01-01 is a valid date in BC?


Sorry, i should explain.

year 0000 is 1 BC
Postgres supports BC dates , as in it actually stores it as "0001-01-01 BC" which when queried as a date returns 0000-01-01, which then rails makes nil

hope it makes sence.


sorry one more thing
when you save,1,1) to rails Postgres stores it as "1001-01-01 BC"
It does conversion based on fact that year 0 doesnt exist. so first year before 1 AD is 1 BC, hence year 0 is 1 BC


Seems fine to me,,0,0) raises ArgumentError: invalid date but,1,1) returns a date.

I'm not sure that Date and DateTime will do sensible things with BC dates, but seems harmless enough to support the values if the database returns them.

@NZKoz NZKoz merged commit 2c08a74 into rails:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 17, 2011
  1. Year 0 should fall to nil only if month and day are also 0

    Kirill Radzikhovskyy authored
This page is out of date. Refresh to see the latest.
Showing with 1 addition and 1 deletion.
  1. +1 −1  activerecord/lib/active_record/connection_adapters/column.rb
2  activerecord/lib/active_record/connection_adapters/column.rb
@@ -189,7 +189,7 @@ def new_date(year, mon, mday)
def new_time(year, mon, mday, hour, min, sec, microsec)
# Treat 0000-00-00 00:00:00 as nil.
- return nil if year.nil? || year == 0
+ return nil if year.nil? || (year == 0 && mon == 0 && mday == 0)
Time.time_with_datetime_fallback(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
Something went wrong with that request. Please try again.