Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8720

Show
Ignore:
Timestamp:
01/25/08 18:23:22 (5 months ago)
Author:
gbuesing
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r8719 r8720  
    11*SVN* 
     2 
     3* Adding UTC zone to TimeZone; TimeWithZone no longer has to fake UTC zone with nil [Geoff Buesing] 
    24 
    35* Time.get_zone refactored to private method, given that the encapsulated logic is only useful internally [Geoff Buesing] 
  • trunk/activesupport/lib/active_support/time_with_zone.rb

    r8711 r8720  
    66    attr_reader :time_zone 
    77   
    8     def initialize(utc_time, time_zone = nil, local_time = nil) 
     8    def initialize(utc_time, time_zone, local_time = nil) 
    99      @utc = utc_time 
    1010      @time = local_time 
     
    1414    # Returns a Time instance that represents the time in time_zone 
    1515    def time 
    16       @time ||= utc? ? @utc : time_zone.utc_to_local(@utc) 
     16      @time ||= time_zone.utc_to_local(@utc) 
    1717    end 
    1818 
    1919    # Returns a Time instance that represents the time in UTC 
    2020    def utc 
    21       @utc ||= utc? ? @time : time_zone.local_to_utc(@time) 
     21      @utc ||= time_zone.local_to_utc(@time) 
    2222    end 
    2323    alias_method :comparable_time, :utc 
     
    5454   
    5555    def dst? 
    56       utc? ? false : period.dst? 
     56      period.dst? 
    5757    end 
    5858   
    59     # The TimeZone class has no zone for UTC, so this class uses the absence of a time zone to indicate UTC 
    6059    def utc? 
    61       !time_zone 
     60      time_zone.name == 'UTC' 
    6261    end 
    6362   
    6463    def utc_offset 
    65       utc? ? 0 : period.utc_total_offset 
     64      period.utc_total_offset 
    6665    end 
    6766   
     
    7271    # Time uses #zone to display the time zone abbreviation, so we're duck-typing it 
    7372    def zone 
    74       utc? ? 'UTC' : period.abbreviation.to_s 
     73      period.abbreviation.to_s 
    7574    end 
    7675   
  • trunk/activesupport/lib/active_support/values/time_zone.rb

    r8716 r8720  
    4141    "Casablanca"                   => "Africa/Casablanca", 
    4242    "Monrovia"                     => "Africa/Monrovia", 
     43    "UTC"                          => "UTC", 
    4344    "Belgrade"                     => "Europe/Belgrade", 
    4445    "Bratislava"                   => "Europe/Bratislava", 
     
    260261         [ -3_600, "Azores", "Cape Verde Is." ], 
    261262         [      0, "Dublin", "Edinburgh", "Lisbon", "London", "Casablanca", 
    262                    "Monrovia" ], 
     263                   "Monrovia", "UTC" ], 
    263264         [  3_600, "Belgrade", "Bratislava", "Budapest", "Ljubljana", "Prague", 
    264265                   "Sarajevo", "Skopje", "Warsaw", "Zagreb", "Brussels", 
  • trunk/activesupport/test/core_ext/date_time_ext_test.rb

    r8711 r8720  
    268268   
    269269  def test_compare_with_time_with_zone 
    270     assert_equal  1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59)
    271     assert_equal  0, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0)
    272     assert_equal(-1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1) )) 
     270    assert_equal  1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), TimeZone['UTC']
     271    assert_equal  0, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), TimeZone['UTC']
     272    assert_equal(-1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] )) 
    273273  end 
    274274 
  • trunk/activesupport/test/core_ext/time_ext_test.rb

    r8715 r8720  
    451451   
    452452  def test_compare_with_time_with_zone 
    453     assert_equal  1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59)
    454     assert_equal  0, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0)
    455     assert_equal(-1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1) )) 
     453    assert_equal  1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), TimeZone['UTC']
     454    assert_equal  0, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), TimeZone['UTC']
     455    assert_equal(-1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] )) 
    456456  end 
    457457 
  • trunk/activesupport/test/core_ext/time_with_zone_test.rb

    r8718 r8720  
    4747    def test_utc? 
    4848      assert_equal false, @twz.utc? 
    49       assert_equal true, ActiveSupport::TimeWithZone.new(Time.utc(2000)).utc? 
     49      assert_equal true, ActiveSupport::TimeWithZone.new(Time.utc(2000), TimeZone['UTC']).utc? 
    5050    end 
    5151       
     
    102102 
    103103    def test_compare_with_time_with_zone 
    104       assert_equal  1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59)
    105       assert_equal  0, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0)
    106       assert_equal(-1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1) )) 
     104      assert_equal  1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), TimeZone['UTC']
     105      assert_equal  0, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), TimeZone['UTC']
     106      assert_equal(-1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] )) 
    107107    end 
    108108       
     
    160160    def test_in_time_zone 
    161161      silence_warnings do # silence warnings raised by tzinfo gem 
    162         Time.use_zone 'Eastern Time (US & Canada)' do 
     162        Time.use_zone 'Eastern Time (US & Canada)' do # Time.zone will not affect #in_time_zone(zone) 
    163163          assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone('Alaska').inspect 
    164164          assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone('Alaska').inspect 
    165165          assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_time_zone('Hawaii').inspect 
    166166          assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_time_zone('Hawaii').inspect 
     167          assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @t.in_time_zone('UTC').inspect 
     168          assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @dt.in_time_zone('UTC').inspect 
    167169        end 
    168170      end 
     
    186188    def test_change_time_zone 
    187189      silence_warnings do # silence warnings raised by tzinfo gem 
    188         Time.use_zone 'Eastern Time (US & Canada)' do 
     190        Time.use_zone 'Eastern Time (US & Canada)' do # Time.zone will not affect #change_time_zone(zone) 
    189191          assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @t.change_time_zone('Alaska').inspect 
    190192          assert_equal 'Sat, 01 Jan 2000 00:00:00 AKST -09:00', @dt.change_time_zone('Alaska').inspect 
    191193          assert_equal 'Sat, 01 Jan 2000 00:00:00 HST -10:00', @t.change_time_zone('Hawaii').inspect 
    192194          assert_equal 'Sat, 01 Jan 2000 00:00:00 HST -10:00', @dt.change_time_zone('Hawaii').inspect 
     195          assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @t.change_time_zone('UTC').inspect 
     196          assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @dt.change_time_zone('UTC').inspect 
    193197        end 
    194198      end