convert times to local when quoting for db

This commit is contained in:
Charlie Somerville
2014-09-19 17:50:29 +10:00
parent 923ee6adf8
commit 78e6c36ba3
2 changed files with 14 additions and 9 deletions

View File

@@ -100,14 +100,6 @@ module ActiveRecord
end
def quoted_date(value)
if value.acts_like?(:time)
zone_conversion_method = ActiveRecord::Base.default_timezone == :utc ? :getutc : :getlocal
if value.respond_to?(zone_conversion_method)
value = value.send(zone_conversion_method)
end
end
value.to_s(:db)
end
end

View File

@@ -4,7 +4,20 @@ require 'active_support/values/time_zone'
class Time
DATE_FORMATS = {
:db => "%Y-%m-%d %H:%M:%S",
:db => lambda { |time|
time = time.utc.to_time
if !defined?(ActiveRecord::Base.default_timezone) || ActiveRecord::Base.default_timezone == :local
# our DB is in local time (ugh), so make sure the time object is
# converted to local time before converting it to a db string
#
# also we have to do this ridiculous dance to ensure that we can
# turn any given DateTime object into something in localtime.
time = time.getlocal
end
time.strftime("%Y-%m-%d %H:%M:%S")
},
:number => "%Y%m%d%H%M%S",
:time => "%H:%M",
:short => "%d %b %H:%M",