From 47576a646bd99d431f4217b20b7625bdd3444171 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Mon, 5 Nov 2007 22:29:11 +0000 Subject: Cater for DST changes when converting Times to DateTimes. Closes #10068 [gbuesing] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8076 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/active_support/core_ext/date_time/calculations.rb | 10 ++++++++++ .../lib/active_support/core_ext/date_time/conversions.rb | 5 ++--- activesupport/lib/active_support/core_ext/time/calculations.rb | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) (limited to 'activesupport/lib/active_support/core_ext') diff --git a/activesupport/lib/active_support/core_ext/date_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_time/calculations.rb index b672ada7b9..2e85d14579 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -5,6 +5,16 @@ module ActiveSupport #:nodoc: module DateTime #:nodoc: # Enables the use of time calculations within DateTime itself module Calculations + def self.included(base) #:nodoc: + base.extend ClassMethods + end + + module ClassMethods + # DateTimes aren't aware of DST rules, so use a consistent non-DST offset when creating a DateTime with an offset in the local zone + def local_offset + ::Time.local(2007).utc_offset.to_r / 86400 + end + end # Seconds since midnight: DateTime.now.seconds_since_midnight def seconds_since_midnight diff --git a/activesupport/lib/active_support/core_ext/date_time/conversions.rb b/activesupport/lib/active_support/core_ext/date_time/conversions.rb index 776f1806f9..45d09f344d 100644 --- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb @@ -35,10 +35,9 @@ module ActiveSupport #:nodoc: end # Attempts to convert self to a Ruby Time object; returns self if out of range of Ruby Time class - # If self.offset is 0, then will attempt to cast as a utc time; otherwise will attempt to cast in local time zone + # If self has an offset other than 0, self will just be returned unaltered, since there's no clean way to map it to a Time def to_time - method = if self.offset == 0 then 'utc' else 'local' end - ::Time.send(method, year, month, day, hour, min, sec) rescue self + self.offset == 0 ? ::Time.utc_time(year, month, day, hour, min, sec) : self end # To be able to keep Times, Dates and DateTimes interchangeable on conversions diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 7f449aee49..19ac8f5a36 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -35,7 +35,7 @@ module ActiveSupport #:nodoc: def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0) ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec) rescue - offset = if utc_or_local.to_sym == :utc then 0 else ::DateTime.now.offset end + offset = utc_or_local.to_sym == :local ? ::DateTime.local_offset : 0 ::DateTime.civil(year, month, day, hour, min, sec, offset, 0) end -- cgit v1.2.3