aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support
diff options
context:
space:
mode:
authorAndrew White <andrew.white@unboxed.co>2016-10-31 17:21:15 +0000
committerAndrew White <andrew.white@unboxed.co>2016-10-31 17:25:43 +0000
commit8931916f4a1c1d8e70c06063ba63928c5c7eab1e (patch)
tree936a2edb3fcacb16cc7357309b682781d1ce4f9e /activesupport/lib/active_support
parent3c9eb704e8b7fc175728f0340043b888120764cc (diff)
downloadrails-8931916f4a1c1d8e70c06063ba63928c5c7eab1e.tar.gz
rails-8931916f4a1c1d8e70c06063ba63928c5c7eab1e.tar.bz2
rails-8931916f4a1c1d8e70c06063ba63928c5c7eab1e.zip
Ensure duration parsing is consistent across DST changes
Previously `ActiveSupport::Duration.parse` used `Time.current` and `Time#advance` to calculate the number of seconds in the duration from an arbitrary collection of parts. However as `advance` tries to be consistent across DST boundaries this meant that either the duration was shorter or longer depending on the time of year. This was fixed by using an absolute reference point in UTC which isn't subject to DST transitions. An arbitrary date of Jan 1st, 2000 was chosen for no other reason that it seemed appropriate. Additionally, duration parsing should now be marginally faster as we are no longer creating instances of `ActiveSupport::TimeWithZone` every time we parse a duration string. Fixes #26941.
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r--activesupport/lib/active_support/duration.rb5
1 files changed, 3 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb
index 862dabb1e8..82322291d0 100644
--- a/activesupport/lib/active_support/duration.rb
+++ b/activesupport/lib/active_support/duration.rb
@@ -7,6 +7,8 @@ module ActiveSupport
#
# 1.month.ago # equivalent to Time.now.advance(months: -1)
class Duration
+ EPOCH = ::Time.utc(2000)
+
attr_accessor :value, :parts
autoload :ISO8601Parser, "active_support/duration/iso8601_parser"
@@ -140,8 +142,7 @@ module ActiveSupport
# If invalid string is provided, it will raise +ActiveSupport::Duration::ISO8601Parser::ParsingError+.
def self.parse(iso8601duration)
parts = ISO8601Parser.new(iso8601duration).parse!
- time = ::Time.current
- new(time.advance(parts) - time, parts)
+ new(EPOCH.advance(parts) - EPOCH, parts)
end
# Build ISO 8601 Duration string for this duration.