Merge pull request #30 from github/3-2-github+timezones

[3.2] Unfuck timezones for db quoting
This commit is contained in:
Charlie Somerville
2014-09-19 18:25:37 +10:00
6 changed files with 24 additions and 15 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.dup.utc.to_time.utc
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",

View File

@@ -3,14 +3,14 @@ require 'active_support/time'
class DateTimeExtCalculationsTest < Test::Unit::TestCase
def test_to_s
datetime = DateTime.new(2005, 2, 21, 14, 30, 0, 0)
datetime = Time.new(2005, 2, 21, 14, 30, 0).to_datetime
assert_equal "2005-02-21 14:30:00", datetime.to_s(:db)
assert_equal "14:30", datetime.to_s(:time)
assert_equal "21 Feb 14:30", datetime.to_s(:short)
assert_equal "February 21, 2005 14:30", datetime.to_s(:long)
assert_equal "Mon, 21 Feb 2005 14:30:00 +0000", datetime.to_s(:rfc822)
assert_equal "Mon, 21 Feb 2005 14:30:00 #{datetime.formatted_offset(false)}", datetime.to_s(:rfc822)
assert_equal "February 21st, 2005 14:30", datetime.to_s(:long_ordinal)
assert_match(/^2005-02-21T14:30:00(Z|\+00:00)$/, datetime.to_s)
assert_equal "2005-02-21T14:30:00#{datetime.formatted_offset(true)}", datetime.to_s
end
def test_readable_inspect

View File

@@ -9,7 +9,7 @@ class RangeTest < Test::Unit::TestCase
end
def test_to_s_from_times
date_range = Time.utc(2005, 12, 10, 15, 30)..Time.utc(2005, 12, 10, 17, 30)
date_range = Time.new(2005, 12, 10, 15, 30)..Time.new(2005, 12, 10, 17, 30)
assert_equal "BETWEEN '2005-12-10 15:30:00' AND '2005-12-10 17:30:00'", date_range.to_s(:db)
end

View File

@@ -541,7 +541,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
time = Time.utc(2005, 2, 21, 17, 44, 30.12345678901)
assert_equal time.to_default_s, time.to_s
assert_equal time.to_default_s, time.to_s(:doesnt_exist)
assert_equal "2005-02-21 17:44:30", time.to_s(:db)
assert_equal "21 Feb 17:44", time.to_s(:short)
assert_equal "17:44", time.to_s(:time)
assert_equal "20050221174430", time.to_s(:number)
@@ -555,6 +554,10 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
assert_equal "Thu, 05 Feb 2009 14:30:05 -0600", Time.local(2009, 2, 5, 14, 30, 5).to_s(:rfc822)
assert_equal "Mon, 09 Jun 2008 04:05:01 -0500", Time.local(2008, 6, 9, 4, 5, 1).to_s(:rfc822)
end
# this one's a special snowflake because the db time is local
time = Time.new(2005, 2, 21, 17, 44, 30.12345678901)
assert_equal "2005-02-21 17:44:30", time.to_s(:db)
end
def test_custom_date_format

View File

@@ -107,7 +107,8 @@ class TimeWithZoneTest < Test::Unit::TestCase
end
def test_to_s_db
assert_equal '2000-01-01 00:00:00', @twz.to_s(:db)
# This test assumes the local timezone of the machine is Pacific.
assert_equal '1999-12-31 16:00:00', @twz.to_s(:db)
end
def test_xmlschema