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

Changeset 9107

Show
Ignore:
Timestamp:
03/28/08 04:06:47 (4 months ago)
Author:
gbuesing
Message:

Time.zone= accepts TZInfo::Timezone instances and Olson identifiers; wraps result in TimeZone instance

Files:

Legend:

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

    r9106 r9107  
    11*SVN* 
     2 
     3* Time.zone= accepts TZInfo::Timezone instances and Olson identifiers; wraps result in TimeZone instance [Geoff Buesing]  
    24 
    35* TimeWithZone time conversions don't need to be wrapped in TimeOrDateTime, because TZInfo does this internally [Geoff Buesing] 
  • trunk/activesupport/lib/active_support/core_ext/time/zones.rb

    r9093 r9107  
    3939          private 
    4040            def get_zone(time_zone) 
    41               return time_zone if time_zone.nil? || time_zone.respond_to?(:period_for_local) 
    42               TimeZone[time_zone] 
     41              return time_zone if time_zone.nil? || time_zone.is_a?(TimeZone) 
     42              # lookup timezone based on identifier (unless we've been passed a TZInfo::Timezone) 
     43              unless time_zone.respond_to?(:period_for_local) 
     44                time_zone = TimeZone[time_zone] || TZInfo::Timezone.get(time_zone) rescue nil 
     45              end 
     46              # Return if a TimeZone instance, or wrap in a TimeZone instance if a TZInfo::Timezone 
     47              if time_zone 
     48                time_zone.is_a?(TimeZone) ? time_zone : TimeZone.create(time_zone.name, nil, time_zone) 
     49              end 
    4350            end 
    4451        end 
  • trunk/activesupport/lib/active_support/values/time_zone.rb

    r9071 r9107  
    146146 
    147147  include Comparable 
    148   attr_reader :name, :utc_offset 
     148  attr_reader :name 
    149149 
    150150  # Create a new TimeZone object with the given name and offset. The 
     
    152152  # (GMT). Seconds were chosen as the offset unit because that is the unit that 
    153153  # Ruby uses to represent time zone offsets (see Time#utc_offset). 
    154   def initialize(name, utc_offset
     154  def initialize(name, utc_offset, tzinfo = nil
    155155    @name = name 
    156156    @utc_offset = utc_offset 
     157    @tzinfo = tzinfo 
     158  end 
     159   
     160  def utc_offset 
     161    @utc_offset ||= tzinfo.current_period.utc_offset 
    157162  end 
    158163 
  • trunk/activesupport/test/core_ext/time_with_zone_test.rb

    r9106 r9107  
    484484    end 
    485485     
     486    def test_time_zone_setter_with_tzinfo_timezone_object_wraps_in_rails_time_zone 
     487      silence_warnings do # silence warnings raised by tzinfo gem 
     488        tzinfo = TZInfo::Timezone.get('America/New_York') 
     489        Time.zone = tzinfo 
     490        assert_kind_of TimeZone, Time.zone 
     491        assert_equal tzinfo, Time.zone.tzinfo 
     492        assert_equal 'America/New_York', Time.zone.name 
     493        assert_equal(-18_000, Time.zone.utc_offset) 
     494      end 
     495    end 
     496     
     497    def test_time_zone_setter_with_tzinfo_timezone_identifier_does_lookup_and_wraps_in_rails_time_zone 
     498      silence_warnings do # silence warnings raised by tzinfo gem 
     499        Time.zone = 'America/New_York' 
     500        assert_kind_of TimeZone, Time.zone 
     501        assert_equal 'America/New_York', Time.zone.tzinfo.name 
     502        assert_equal 'America/New_York', Time.zone.name 
     503        assert_equal(-18_000, Time.zone.utc_offset) 
     504      end 
     505    end 
     506     
     507    def test_time_zone_setter_with_non_identifying_argument_returns_nil 
     508      Time.zone = 'foo' 
     509      assert_equal nil, Time.zone 
     510      Time.zone = -15.hours 
     511      assert_equal nil, Time.zone 
     512    end 
     513     
    486514    protected 
    487515      def with_env_tz(new_tz = 'US/Eastern') 
  • trunk/activesupport/test/time_zone_test.rb

    r9071 r9107  
    193193      end 
    194194    end 
     195     
     196    def test_utc_offset_lazy_loaded_from_tzinfo_when_not_passed_in_to_initialize 
     197      silence_warnings do # silence warnings raised by tzinfo gem 
     198        tzinfo = TZInfo::Timezone.get('America/New_York') 
     199        zone = TimeZone.create(tzinfo.name, nil, tzinfo) 
     200        assert_equal nil, zone.instance_variable_get('@utc_offset') 
     201        assert_equal(-18_000, zone.utc_offset) 
     202      end 
     203    end 
    195204  end 
    196205