Check the database timezone at startup

Posted on July 02, 2008

If your application is dealing with different time zones and code which relies on them, it could be handy to ensure the database is using the time zone you expect it to.

drop something like this in a rails initializer:

#
# Check that that database is running in UTC
# and stop if it's not.
#
db_now = ActiveRecord::Base.connection.execute( 
    "select now() as now" ).fetch_hash['now']
utc_now = Time.now.utc.to_s(:db)

if db_now != utc_now
  raise LoadError, "ERROR: Database is not in UTC" 
end

btw: you should thank david, not me.