Skip to content
This repository was archived by the owner on May 5, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion activerecord/test/cases/date_time_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_saves_both_date_and_time
task.save!

# check against Time.local_time, since some platforms will return a Time instead of a DateTime
assert_equal Time.local_time(*time_values), Task.find(task.id).starting
assert_equal DateTime.civil(*time_values), Task.find(task.id).starting
end

def test_assign_empty_date_time
Expand Down
11 changes: 9 additions & 2 deletions activesupport/lib/active_support/core_ext/time/conversions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ module Time #:nodoc:
# Converting times to formatted strings, dates, and datetimes.
module Conversions
DATE_FORMATS = {
:db => "%Y-%m-%d %H:%M:%S",
:db => lambda { |time|
# 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.utc.to_time.getlocal.strftime("%Y-%m-%d %H:%M:%S")
},
:number => "%Y%m%d%H%M%S",
:time => "%H:%M",
:short => "%d %b %H:%M",
Expand Down Expand Up @@ -48,7 +55,7 @@ def to_formatted_s(format = :default)
return to_default_s unless formatter = DATE_FORMATS[format]
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
end

# Returns the UTC offset as an +HH:MM formatted string.
#
# Time.local(2000).formatted_offset # => "-06:00"
Expand Down
10 changes: 6 additions & 4 deletions activesupport/test/core_ext/date_time_ext_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

class DateTimeExtCalculationsTest < Test::Unit::TestCase
def test_to_s
datetime = DateTime.new(2005, 2, 21, 14, 30, 0, 0)
# this is how you create a local DateTime. lol.
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
Expand Down Expand Up @@ -229,7 +231,7 @@ def test_past_with_offset
assert_equal false, DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)).past?
assert_equal false, DateTime.civil(2005,2,10,15,30,46, Rational(-18000, 86400)).past?
end

def test_past_without_offset
DateTime.stubs(:current).returns(DateTime.civil(2005,2,10,15,30,45, Rational(-18000, 86400)))
assert_equal true, DateTime.civil(2005,2,10,20,30,44).past?
Expand Down
2 changes: 1 addition & 1 deletion activesupport/test/core_ext/range_ext_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_to_s_from_dates
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

Expand Down
7 changes: 5 additions & 2 deletions activesupport/test/core_ext/time_ext_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,6 @@ def test_to_s
time = Time.utc(2005, 2, 21, 17, 44, 30)
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 "February 21, 2005 17:44", time.to_s(:long)
Expand All @@ -469,6 +468,10 @@ def test_to_s
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)
assert_equal "2005-02-21 17:44:30", time.to_s(:db)
end

def test_custom_date_format
Expand Down Expand Up @@ -688,7 +691,7 @@ def test_compare_with_time_with_zone
def test_minus_with_time_with_zone
assert_equal 86_400.0, Time.utc(2000, 1, 2) - ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), ActiveSupport::TimeZone['UTC'] )
end

def test_minus_with_datetime
assert_equal 86_400.0, Time.utc(2000, 1, 2) - DateTime.civil(2000, 1, 1)
end
Expand Down
5 changes: 3 additions & 2 deletions activesupport/test/core_ext/time_with_zone_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def test_to_formatted_s
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
Expand Down Expand Up @@ -696,7 +697,7 @@ def test_advance_1_year_during_dst
assert_equal "Sun, 15 Jul 2007 10:30:00 EDT -04:00", twz.years_ago(1).inspect
assert_equal "Sun, 15 Jul 2007 10:30:00 EDT -04:00", (twz - 1.year).inspect
end

protected
def with_env_tz(new_tz = 'US/Eastern')
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
Expand Down