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
This commit is contained in:
parent
163a4c6f45
commit
8831180b15
|
@ -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 @@ 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?
|
||||
|
@ -80,6 +80,15 @@ module ActiveSupport
|
|||
%("#{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)
|
||||
return utc.to_s(format) if format == :db
|
||||
|
@ -108,6 +117,18 @@ module ActiveSupport
|
|||
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
|
||||
self
|
||||
|
|
|
@ -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)
|
||||
|
@ -127,6 +135,22 @@ uses_tzinfo 'TimeWithZoneTest' do
|
|||
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
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue