From 59183eec6f8d3e5d1ac5046dbd4f5cefc7041256 Mon Sep 17 00:00:00 2001 From: Geoff Buesing Date: Fri, 28 Mar 2008 04:06:47 +0000 Subject: Time.zone= accepts TZInfo::Timezone instances and Olson identifiers; wraps result in TimeZone instance git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9107 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/lib/active_support/core_ext/time/zones.rb | 11 +++++++++-- activesupport/lib/active_support/values/time_zone.rb | 9 +++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/time/zones.rb b/activesupport/lib/active_support/core_ext/time/zones.rb index e7610d144f..3096427904 100644 --- a/activesupport/lib/active_support/core_ext/time/zones.rb +++ b/activesupport/lib/active_support/core_ext/time/zones.rb @@ -38,8 +38,15 @@ module ActiveSupport #:nodoc: private def get_zone(time_zone) - return time_zone if time_zone.nil? || time_zone.respond_to?(:period_for_local) - TimeZone[time_zone] + return time_zone if time_zone.nil? || time_zone.is_a?(TimeZone) + # lookup timezone based on identifier (unless we've been passed a TZInfo::Timezone) + unless time_zone.respond_to?(:period_for_local) + time_zone = TimeZone[time_zone] || TZInfo::Timezone.get(time_zone) rescue nil + end + # Return if a TimeZone instance, or wrap in a TimeZone instance if a TZInfo::Timezone + if time_zone + time_zone.is_a?(TimeZone) ? time_zone : TimeZone.create(time_zone.name, nil, time_zone) + end end end diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 91f0394472..9b9602432c 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -145,15 +145,20 @@ class TimeZone } include Comparable - attr_reader :name, :utc_offset + attr_reader :name # Create a new TimeZone object with the given name and offset. The # offset is the number of seconds that this time zone is offset from UTC # (GMT). Seconds were chosen as the offset unit because that is the unit that # Ruby uses to represent time zone offsets (see Time#utc_offset). - def initialize(name, utc_offset) + def initialize(name, utc_offset, tzinfo = nil) @name = name @utc_offset = utc_offset + @tzinfo = tzinfo + end + + def utc_offset + @utc_offset ||= tzinfo.current_period.utc_offset end # Returns the offset of this time zone as a formatted string, of the -- cgit v1.2.3