From 52b57c662c129d72d44fa1ab9920a752c1973482 Mon Sep 17 00:00:00 2001 From: Geoff Buesing Date: Tue, 11 Mar 2008 04:26:20 +0000 Subject: TimeWithZone instances correctly enforce DST rules. Adding TimeZone#period_for_utc git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9006 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/lib/active_support/time_with_zone.rb | 26 ++++++++++------------ .../lib/active_support/values/time_zone.rb | 5 +++++ 2 files changed, 17 insertions(+), 14 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index c6fa118b55..0f5d0a2ee3 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -27,7 +27,7 @@ module ActiveSupport # Returns the underlying TZInfo::TimezonePeriod for the local time def period - @period ||= get_period_for_local + @period ||= time_zone.period_for_utc(utc) end # Returns the simultaneous time in the specified zone @@ -150,6 +150,15 @@ module ActiveSupport end end + %w(asctime day hour min mon sec usec wday yday year).each do |name| + define_method(name) do + time.__send__(name) + end + end + alias_method :ctime, :asctime + alias_method :mday, :day + alias_method :month, :mon + def to_a time.to_a[0, 8].push(dst?, zone) end @@ -206,20 +215,9 @@ module ActiveSupport # Send the missing method to time instance, and wrap result in a new TimeWithZone with the existing time_zone def method_missing(sym, *args, &block) - result = time.__send__(sym, *args, &block) - result = result.change_time_zone(time_zone) if result.acts_like?(:time) + result = utc.__send__(sym, *args, &block) + result = result.in_time_zone(time_zone) if result.acts_like?(:time) result end - - private - def get_period_for_local - t = time - begin - time_zone.period_for_local(t, true) - rescue ::TZInfo::PeriodNotFound # failover logic from TzTime - t -= 1.hour - retry - end - end end end diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 4f2fc5e44b..ced823f052 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -211,6 +211,11 @@ class TimeZone tzinfo.local_to_utc(time, dst) end + # Available so that TimeZone instances respond like TZInfo::Timezone instances + def period_for_utc(time) + tzinfo.period_for_utc(time) + end + # Available so that TimeZone instances respond like TZInfo::Timezone instances def period_for_local(time, dst=true) tzinfo.period_for_local(time, dst) -- cgit v1.2.3