Permalink
Browse files

Adding TimeWithZone #to_a, #to_f, #to_i, #httpdate, #rfc2822

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8852 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 163a4c6 commit 8831180b152007c52abb2a03bb351ebc7079c4c4 @gbuesing gbuesing committed Feb 10, 2008
View
@@ -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]
@@ -44,7 +44,7 @@ def change_time_zone(new_zone)
# 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 @@ def xmlschema
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 @@ def <=>(other)
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
@@ -82,6 +82,14 @@ def test_xmlschema
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 @@ def test_minus_with_time_with_zone
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

0 comments on commit 8831180

Please sign in to comment.