diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2013-03-13 04:45:47 +0000 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2013-03-13 05:40:25 +0000 |
commit | ba910d76509158d531c637c4ab777beb8b20e53d (patch) | |
tree | 28deec6696e38e1e341e3eea903cac5b4732182c /activesupport | |
parent | a0c3c1e1f796c215685e4e4bcd3e8c9178936492 (diff) | |
download | rails-ba910d76509158d531c637c4ab777beb8b20e53d.tar.gz rails-ba910d76509158d531c637c4ab777beb8b20e53d.tar.bz2 rails-ba910d76509158d531c637c4ab777beb8b20e53d.zip |
Fix `ActiveSupport::TimeZone.parse` when time is a local DST jump
The previous implementation `ActiveSupport::TimeZone.parse` used `Time.parse`
which applies the system time DST rules to the parsed time. Instead we now
use `Time.utc` and manually apply the offset.
Backport tests from:
005d910624bbfa724b638426a000c8074d4201a2
c89b6c4cdce7ee55ed3665c099d914222fe0344a
03becb13099c439f6aea5058546bc8b0b19b8db8
Fixes #9678.
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activesupport/lib/active_support/values/time_zone.rb | 22 | ||||
-rw-r--r-- | activesupport/test/time_zone_test.rb | 56 |
3 files changed, 76 insertions, 7 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 528de79cf6..bade1ea4e2 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,6 +1,9 @@ ## unreleased ## -* No changes. +* Fix `ActiveSupport::TimeZone.parse` when time is at a local DST jump. + Fixes #9678. + + *Andrew White* ## Rails 3.2.13.rc1 (Feb 17, 2013) ## diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index c7d8fc2e17..f935180036 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -268,13 +268,23 @@ module ActiveSupport # Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00 # Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00 def parse(str, now=now) - date_parts = Date._parse(str) - return if date_parts.blank? - time = Time.parse(str, now) rescue DateTime.parse(str) - if date_parts[:offset].nil? - ActiveSupport::TimeWithZone.new(nil, self, time) + parts = Date._parse(str, false) + return if parts.empty? + + time = Time.utc( + parts.fetch(:year, now.year), + parts.fetch(:mon, now.month), + parts.fetch(:mday, now.day), + parts.fetch(:hour, 0), + parts.fetch(:min, 0), + parts.fetch(:sec, 0), + parts.fetch(:sec_fraction, 0) * 1000000 + ) + + if parts[:offset] + TimeWithZone.new(time - parts[:offset], self) else - time.in_time_zone(self) + TimeWithZone.new(nil, self, time) end end diff --git a/activesupport/test/time_zone_test.rb b/activesupport/test/time_zone_test.rb index 8ecfc1e47e..bd4bfca82c 100644 --- a/activesupport/test/time_zone_test.rb +++ b/activesupport/test/time_zone_test.rb @@ -198,6 +198,62 @@ class TimeZoneTest < Test::Unit::TestCase assert_equal Time.utc(1999,12,31,19), twz.time end + def test_parse_should_not_black_out_system_timezone_dst_jump + with_env_tz('EET') do + zone = ActiveSupport::TimeZone['Pacific Time (US & Canada)'] + twz = zone.parse('2012-03-25 03:29:00') + assert_equal [0, 29, 3, 25, 3, 2012], twz.to_a[0,6] + end + end + + def test_parse_should_black_out_app_timezone_dst_jump + with_env_tz('EET') do + zone = ActiveSupport::TimeZone['Pacific Time (US & Canada)'] + twz = zone.parse('2012-03-11 02:29:00') + assert_equal [0, 29, 3, 11, 3, 2012], twz.to_a[0,6] + end + end + + def test_parse_with_javascript_date + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + twz = zone.parse("Mon May 28 2012 00:00:00 GMT-0700 (PDT)") + assert_equal Time.utc(2012, 5, 28, 7, 0, 0), twz.utc + end + + def test_parse_with_missing_time_components + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + zone.stubs(:now).returns zone.local(1999, 12, 31, 12, 59, 59) + twz = zone.parse('2012-12-01') + assert_equal Time.utc(2012, 12, 1), twz.time + end + + def test_parse_doesnt_use_local_dst + with_env_tz 'US/Eastern' do + zone = ActiveSupport::TimeZone['UTC'] + twz = zone.parse('2013-03-10 02:00:00') + assert_equal Time.utc(2013, 3, 10, 2, 0, 0), twz.time + end + end + + def test_parse_handles_dst_jump + with_env_tz 'US/Eastern' do + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + twz = zone.parse('2013-03-10 02:00:00') + assert_equal Time.utc(2013, 3, 10, 3, 0, 0), twz.time + end + end + + def test_parse_with_fractional_seconds + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + twz = zone.parse('2013-03-13 00:00:00.000001') + assert_equal 1, twz.usec + + if twz.respond_to?(:nsec) + twz = zone.parse('2013-03-13 00:00:00.000000001') + assert_equal 1, twz.nsec + end + end + def test_utc_offset_lazy_loaded_from_tzinfo_when_not_passed_in_to_initialize tzinfo = TZInfo::Timezone.get('America/New_York') zone = ActiveSupport::TimeZone.create(tzinfo.name, nil, tzinfo) |