diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 6cbef65ae5..6d930c773b 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Adding TimeWithZone #to_a, #to_f, #to_i, #httpdate, #rfc2822 [Geoff Buesing] + * Pruning unneeded TimeWithZone#change_time_zone_to_current [Geoff Buesing] * Time#zone=, #in_time_zone and #change_time_zone accept a Duration [Geoff Buesing] diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 6b9fed4b8f..517b2bac7f 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -44,7 +44,7 @@ module ActiveSupport # Returns a Time.local() instance of the simultaneous time in your system's ENV['TZ'] zone def localtime - utc.dup.localtime # use #dup because Time#localtime is destructive + utc.getlocal end def dst? @@ -79,6 +79,15 @@ module ActiveSupport def to_json(options = nil) %("#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}") end + + def httpdate + utc.httpdate + end + + def rfc2822 + to_s(:rfc822) + end + alias_method :rfc822, :rfc2822 # :db format outputs time in UTC; all others output time in local. Uses TimeWithZone's strftime, so %Z and %z work correctly def to_s(format = :default) @@ -107,6 +116,18 @@ module ActiveSupport def -(other) other.acts_like?(:time) ? utc - other : method_missing(:-, other) end + + def to_a + time.to_a[0, 8].push(dst?, zone) + end + + def to_f + utc.to_f + end + + def to_i + utc.to_i + end # A TimeProxy acts like a Time, so just return self def to_time diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index cc7a55b2ee..13ebfa644c 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -82,6 +82,14 @@ uses_tzinfo 'TimeWithZoneTest' do assert_equal "1999-12-31T19:00:00-05:00", @twz.xmlschema end + def test_httpdate + assert_equal 'Sat, 01 Jan 2000 00:00:00 GMT', @twz.httpdate + end + + def test_rfc2822 + assert_equal "Fri, 31 Dec 1999 19:00:00 -0500", @twz.rfc2822 + end + def test_compare_with_time assert_equal 1, @twz <=> Time.utc(1999, 12, 31, 23, 59, 59) assert_equal 0, @twz <=> Time.utc(2000, 1, 1, 0, 0, 0) @@ -126,6 +134,22 @@ uses_tzinfo 'TimeWithZoneTest' do twz2 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), TimeZone['UTC'] ) assert_equal 86_400.0, twz2 - twz1 end + + def test_to_a + assert_equal [45, 30, 5, 1, 2, 2000, 2, 32, false, "HST"], ActiveSupport::TimeWithZone.new( Time.utc(2000, 2, 1, 15, 30, 45), TimeZone['Hawaii'] ).to_a + end + + def test_to_f + result = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), TimeZone['Hawaii'] ).to_f + assert_equal 946684800.0, result + assert result.is_a?(Float) + end + + def test_to_i + result = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), TimeZone['Hawaii'] ).to_i + assert_equal 946684800, result + assert result.is_a?(Integer) + end def test_to_time assert_equal @twz, @twz.to_time