1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Adding UTC zone to TimeZone; TimeWithZone no longer has to fake UTC zone with nil

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8720 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Geoff Buesing 2008-01-25 18:23:22 +00:00
parent a7adca3d3b
commit 4b2ac708b7
6 changed files with 27 additions and 21 deletions

View file

@ -1,5 +1,7 @@
*SVN*
* Adding UTC zone to TimeZone; TimeWithZone no longer has to fake UTC zone with nil [Geoff Buesing]
* Time.get_zone refactored to private method, given that the encapsulated logic is only useful internally [Geoff Buesing]
* Time.zone uses thread-local variable for thread safety. Adding Time.use_zone, for overriding Time.zone locally inside a block. Removing unneeded Time.zone_reset! [Geoff Buesing]

View file

@ -5,7 +5,7 @@ module ActiveSupport
include Comparable
attr_reader :time_zone
def initialize(utc_time, time_zone = nil, local_time = nil)
def initialize(utc_time, time_zone, local_time = nil)
@utc = utc_time
@time = local_time
@time_zone = time_zone
@ -13,12 +13,12 @@ module ActiveSupport
# Returns a Time instance that represents the time in time_zone
def time
@time ||= utc? ? @utc : time_zone.utc_to_local(@utc)
@time ||= time_zone.utc_to_local(@utc)
end
# Returns a Time instance that represents the time in UTC
def utc
@utc ||= utc? ? @time : time_zone.local_to_utc(@time)
@utc ||= time_zone.local_to_utc(@time)
end
alias_method :comparable_time, :utc
@ -53,16 +53,15 @@ module ActiveSupport
end
def dst?
utc? ? false : period.dst?
period.dst?
end
# The TimeZone class has no zone for UTC, so this class uses the absence of a time zone to indicate UTC
def utc?
!time_zone
time_zone.name == 'UTC'
end
def utc_offset
utc? ? 0 : period.utc_total_offset
period.utc_total_offset
end
def formatted_offset(colon = true, alternate_utc_string = nil)
@ -71,7 +70,7 @@ module ActiveSupport
# Time uses #zone to display the time zone abbreviation, so we're duck-typing it
def zone
utc? ? 'UTC' : period.abbreviation.to_s
period.abbreviation.to_s
end
def inspect

View file

@ -40,6 +40,7 @@ class TimeZone
"London" => "Europe/London",
"Casablanca" => "Africa/Casablanca",
"Monrovia" => "Africa/Monrovia",
"UTC" => "UTC",
"Belgrade" => "Europe/Belgrade",
"Bratislava" => "Europe/Bratislava",
"Budapest" => "Europe/Budapest",
@ -259,7 +260,7 @@ class TimeZone
[ -7_200, "Mid-Atlantic" ],
[ -3_600, "Azores", "Cape Verde Is." ],
[ 0, "Dublin", "Edinburgh", "Lisbon", "London", "Casablanca",
"Monrovia" ],
"Monrovia", "UTC" ],
[ 3_600, "Belgrade", "Bratislava", "Budapest", "Ljubljana", "Prague",
"Sarajevo", "Skopje", "Warsaw", "Zagreb", "Brussels",
"Copenhagen", "Madrid", "Paris", "Amsterdam", "Berlin",

View file

@ -267,9 +267,9 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase
end
def test_compare_with_time_with_zone
assert_equal 1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59) )
assert_equal 0, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0) )
assert_equal(-1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1) ))
assert_equal 1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), TimeZone['UTC'] )
assert_equal 0, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), TimeZone['UTC'] )
assert_equal(-1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] ))
end
protected

View file

@ -450,9 +450,9 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
end
def test_compare_with_time_with_zone
assert_equal 1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59) )
assert_equal 0, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0) )
assert_equal(-1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1) ))
assert_equal 1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), TimeZone['UTC'] )
assert_equal 0, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), TimeZone['UTC'] )
assert_equal(-1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] ))
end
protected

View file

@ -46,7 +46,7 @@ uses_tzinfo 'TimeWithZoneTest' do
def test_utc?
assert_equal false, @twz.utc?
assert_equal true, ActiveSupport::TimeWithZone.new(Time.utc(2000)).utc?
assert_equal true, ActiveSupport::TimeWithZone.new(Time.utc(2000), TimeZone['UTC']).utc?
end
def test_formatted_offset
@ -101,9 +101,9 @@ uses_tzinfo 'TimeWithZoneTest' do
end
def test_compare_with_time_with_zone
assert_equal 1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59) )
assert_equal 0, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0) )
assert_equal(-1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1) ))
assert_equal 1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), TimeZone['UTC'] )
assert_equal 0, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), TimeZone['UTC'] )
assert_equal(-1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] ))
end
def test_plus
@ -159,11 +159,13 @@ uses_tzinfo 'TimeWithZoneTest' do
def test_in_time_zone
silence_warnings do # silence warnings raised by tzinfo gem
Time.use_zone 'Eastern Time (US & Canada)' do
Time.use_zone 'Eastern Time (US & Canada)' do # Time.zone will not affect #in_time_zone(zone)
assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone('Alaska').inspect
assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone('Alaska').inspect
assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_time_zone('Hawaii').inspect
assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_time_zone('Hawaii').inspect
assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @t.in_time_zone('UTC').inspect
assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @dt.in_time_zone('UTC').inspect
end
end
end
@ -185,11 +187,13 @@ uses_tzinfo 'TimeWithZoneTest' do
def test_change_time_zone
silence_warnings do # silence warnings raised by tzinfo gem
Time.use_zone 'Eastern Time (US & Canada)' do
Time.use_zone 'Eastern Time (US & Canada)' do # Time.zone will not affect #change_time_zone(zone)
assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @t.change_time_zone('Alaska').inspect
assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @dt.change_time_zone('Alaska').inspect
assert_equal 'Sat, 01 Jan 2000 00:00:00 HST -10:00', @t.change_time_zone('Hawaii').inspect
assert_equal 'Sat, 01 Jan 2000 00:00:00 HST -10:00', @dt.change_time_zone('Hawaii').inspect
assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @t.change_time_zone('UTC').inspect
assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @dt.change_time_zone('UTC').inspect
end
end
end