diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2012-12-01 11:15:29 +0000 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2012-12-01 11:34:21 +0000 |
commit | 005d910624bbfa724b638426a000c8074d4201a2 (patch) | |
tree | 62c03078266ab8470ea4f8414205f75c0800d623 /activesupport/lib | |
parent | 0a1c61106975f458bcfef04a2656464644c75c82 (diff) | |
download | rails-005d910624bbfa724b638426a000c8074d4201a2.tar.gz rails-005d910624bbfa724b638426a000c8074d4201a2.tar.bz2 rails-005d910624bbfa724b638426a000c8074d4201a2.zip |
Make `Time.zone.parse` to work with JavaScript date strings
Chrome, Safari and Firefox serialize Date objects to strings such
as 'Mon May 28 2012 00:00:00 GMT-0700 (PDT)'. When these strings
are parsed the zone is interpreted as 'GMT-0700' which doesn't
exist in the TzInfo list of timezones.
By taking advantage of the improved date/time handling in 1.9.3
we can use `Date._parse` and the `:offset` value which is parsed
correctly.
Three tests were amended to make them pass:
1. test_parse_with_old_date
This needed changing to a different value because the original
value was before EST was adopted so was being changed to a
LMT (Local Mean Time) value after the change. It didn't before
because `DateTime` just has offsets from UTC not timezones.
2. test_parse_should_not_black_out_system_timezone_dst_jump
Changed the implementation of this test as the stubs were
dependent on internal implementation details of the test.
Confirmed that the modified test still failed when the
implementation of `parse` was restored to pre-#5571.
3. test_parse_should_black_out_app_timezone_dst_jump
Ditto.
Closes #5770.
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/values/time_zone.rb | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 0207f53238..1a37d6f2a4 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -278,18 +278,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.empty? - time = Time.parse(str, now) rescue DateTime.parse(str) - - if date_parts[:offset].nil? - if date_parts[:hour] && time.hour != date_parts[:hour] - time = DateTime.parse(str) - end - - ActiveSupport::TimeWithZone.new(nil, self, time) + parts = Date._parse(str, false) + return if parts.empty? + + time = Time.new( + parts.fetch(:year, now.year), + parts.fetch(:mon, now.month), + parts.fetch(:mday, now.day), + parts.fetch(:hour, now.hour), + parts.fetch(:min, now.min), + parts.fetch(:sec, now.sec) + parts.fetch(:sec_fraction, 0), + parts.fetch(:offset, 0) + ) + + if parts[:offset] + TimeWithZone.new(time.utc, self) else - time.in_time_zone(self) + TimeWithZone.new(nil, self, time) end end |