2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-06-30 01:00:04 -04:00
|
|
|
require_relative "core_ext/array/conversions"
|
|
|
|
require_relative "core_ext/module/delegation"
|
|
|
|
require_relative "core_ext/object/acts_like"
|
|
|
|
require_relative "core_ext/string/filters"
|
|
|
|
require_relative "deprecation"
|
2008-11-23 19:10:20 -05:00
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
module ActiveSupport
|
2010-08-14 01:13:00 -04:00
|
|
|
# Provides accurate date and time measurements using Date#advance and
|
2010-07-23 16:22:17 -04:00
|
|
|
# Time#advance, respectively. It mainly supports the methods on Numeric.
|
2007-01-15 01:54:50 -05:00
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# 1.month.ago # equivalent to Time.now.advance(months: -1)
|
2014-08-20 02:56:54 -04:00
|
|
|
class Duration
|
2017-03-15 10:38:21 -04:00
|
|
|
class Scalar < Numeric #:nodoc:
|
|
|
|
attr_reader :value
|
|
|
|
delegate :to_i, :to_f, :to_s, to: :value
|
|
|
|
|
|
|
|
def initialize(value)
|
|
|
|
@value = value
|
|
|
|
end
|
|
|
|
|
|
|
|
def coerce(other)
|
|
|
|
[Scalar.new(other), self]
|
|
|
|
end
|
|
|
|
|
|
|
|
def -@
|
|
|
|
Scalar.new(-value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def <=>(other)
|
|
|
|
if Scalar === other || Duration === other
|
|
|
|
value <=> other.value
|
|
|
|
elsif Numeric === other
|
|
|
|
value <=> other
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def +(other)
|
Fix implicit calculations with scalars and durations
Previously calculations where the scalar is first would be converted
to a duration of seconds but this causes issues with dates being
converted to times, e.g:
Time.zone = "Beijing" # => Asia/Shanghai
date = Date.civil(2017, 5, 20) # => Mon, 20 May 2017
2 * 1.day # => 172800 seconds
date + 2 * 1.day # => Mon, 22 May 2017 00:00:00 CST +08:00
Now the `ActiveSupport::Duration::Scalar` calculation methods will try
to maintain the part structure of the duration where possible, e.g:
Time.zone = "Beijing" # => Asia/Shanghai
date = Date.civil(2017, 5, 20) # => Mon, 20 May 2017
2 * 1.day # => 2 days
date + 2 * 1.day # => Mon, 22 May 2017
Fixes #29160, #28970.
2017-05-20 11:33:09 -04:00
|
|
|
if Duration === other
|
|
|
|
seconds = value + other.parts[:seconds]
|
|
|
|
new_parts = other.parts.merge(seconds: seconds)
|
|
|
|
new_value = value + other.value
|
|
|
|
|
|
|
|
Duration.new(new_value, new_parts)
|
|
|
|
else
|
|
|
|
calculate(:+, other)
|
|
|
|
end
|
2017-03-15 10:38:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def -(other)
|
Fix implicit calculations with scalars and durations
Previously calculations where the scalar is first would be converted
to a duration of seconds but this causes issues with dates being
converted to times, e.g:
Time.zone = "Beijing" # => Asia/Shanghai
date = Date.civil(2017, 5, 20) # => Mon, 20 May 2017
2 * 1.day # => 172800 seconds
date + 2 * 1.day # => Mon, 22 May 2017 00:00:00 CST +08:00
Now the `ActiveSupport::Duration::Scalar` calculation methods will try
to maintain the part structure of the duration where possible, e.g:
Time.zone = "Beijing" # => Asia/Shanghai
date = Date.civil(2017, 5, 20) # => Mon, 20 May 2017
2 * 1.day # => 2 days
date + 2 * 1.day # => Mon, 22 May 2017
Fixes #29160, #28970.
2017-05-20 11:33:09 -04:00
|
|
|
if Duration === other
|
|
|
|
seconds = value - other.parts[:seconds]
|
|
|
|
new_parts = other.parts.map { |part, other_value| [part, -other_value] }.to_h
|
|
|
|
new_parts = new_parts.merge(seconds: seconds)
|
|
|
|
new_value = value - other.value
|
|
|
|
|
|
|
|
Duration.new(new_value, new_parts)
|
|
|
|
else
|
|
|
|
calculate(:-, other)
|
|
|
|
end
|
2017-03-15 10:38:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def *(other)
|
Fix implicit calculations with scalars and durations
Previously calculations where the scalar is first would be converted
to a duration of seconds but this causes issues with dates being
converted to times, e.g:
Time.zone = "Beijing" # => Asia/Shanghai
date = Date.civil(2017, 5, 20) # => Mon, 20 May 2017
2 * 1.day # => 172800 seconds
date + 2 * 1.day # => Mon, 22 May 2017 00:00:00 CST +08:00
Now the `ActiveSupport::Duration::Scalar` calculation methods will try
to maintain the part structure of the duration where possible, e.g:
Time.zone = "Beijing" # => Asia/Shanghai
date = Date.civil(2017, 5, 20) # => Mon, 20 May 2017
2 * 1.day # => 2 days
date + 2 * 1.day # => Mon, 22 May 2017
Fixes #29160, #28970.
2017-05-20 11:33:09 -04:00
|
|
|
if Duration === other
|
|
|
|
new_parts = other.parts.map { |part, other_value| [part, value * other_value] }.to_h
|
|
|
|
new_value = value * other.value
|
|
|
|
|
|
|
|
Duration.new(new_value, new_parts)
|
|
|
|
else
|
|
|
|
calculate(:*, other)
|
|
|
|
end
|
2017-03-15 10:38:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def /(other)
|
Fix implicit calculations with scalars and durations
Previously calculations where the scalar is first would be converted
to a duration of seconds but this causes issues with dates being
converted to times, e.g:
Time.zone = "Beijing" # => Asia/Shanghai
date = Date.civil(2017, 5, 20) # => Mon, 20 May 2017
2 * 1.day # => 172800 seconds
date + 2 * 1.day # => Mon, 22 May 2017 00:00:00 CST +08:00
Now the `ActiveSupport::Duration::Scalar` calculation methods will try
to maintain the part structure of the duration where possible, e.g:
Time.zone = "Beijing" # => Asia/Shanghai
date = Date.civil(2017, 5, 20) # => Mon, 20 May 2017
2 * 1.day # => 2 days
date + 2 * 1.day # => Mon, 22 May 2017
Fixes #29160, #28970.
2017-05-20 11:33:09 -04:00
|
|
|
if Duration === other
|
|
|
|
new_parts = other.parts.map { |part, other_value| [part, value / other_value] }.to_h
|
|
|
|
new_value = new_parts.inject(0) { |total, (part, value)| total + value * Duration::PARTS_IN_SECONDS[part] }
|
|
|
|
|
|
|
|
Duration.new(new_value, new_parts)
|
|
|
|
else
|
|
|
|
calculate(:/, other)
|
|
|
|
end
|
2017-03-15 10:38:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def calculate(op, other)
|
|
|
|
if Scalar === other
|
|
|
|
Scalar.new(value.public_send(op, other.value))
|
|
|
|
elsif Numeric === other
|
|
|
|
Scalar.new(value.public_send(op, other))
|
|
|
|
else
|
|
|
|
raise_type_error(other)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def raise_type_error(other)
|
|
|
|
raise TypeError, "no implicit conversion of #{other.class} into #{self.class}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-12 05:02:16 -05:00
|
|
|
SECONDS_PER_MINUTE = 60
|
|
|
|
SECONDS_PER_HOUR = 3600
|
|
|
|
SECONDS_PER_DAY = 86400
|
|
|
|
SECONDS_PER_WEEK = 604800
|
|
|
|
SECONDS_PER_MONTH = 2629746 # 1/12 of a gregorian year
|
|
|
|
SECONDS_PER_YEAR = 31556952 # length of a gregorian year (365.2425 days)
|
|
|
|
|
2017-01-08 17:43:49 -05:00
|
|
|
PARTS_IN_SECONDS = {
|
2017-01-12 05:02:16 -05:00
|
|
|
seconds: 1,
|
|
|
|
minutes: SECONDS_PER_MINUTE,
|
|
|
|
hours: SECONDS_PER_HOUR,
|
|
|
|
days: SECONDS_PER_DAY,
|
|
|
|
weeks: SECONDS_PER_WEEK,
|
|
|
|
months: SECONDS_PER_MONTH,
|
|
|
|
years: SECONDS_PER_YEAR
|
2017-01-08 17:43:49 -05:00
|
|
|
}.freeze
|
2016-10-31 13:21:15 -04:00
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
attr_accessor :value, :parts
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2016-08-06 11:58:50 -04:00
|
|
|
autoload :ISO8601Parser, "active_support/duration/iso8601_parser"
|
|
|
|
autoload :ISO8601Serializer, "active_support/duration/iso8601_serializer"
|
2014-09-14 09:16:51 -04:00
|
|
|
|
2017-01-12 05:02:16 -05:00
|
|
|
class << self
|
|
|
|
# Creates a new Duration from string formatted according to ISO 8601 Duration.
|
|
|
|
#
|
|
|
|
# See {ISO 8601}[http://en.wikipedia.org/wiki/ISO_8601#Durations] for more information.
|
|
|
|
# This method allows negative parts to be present in pattern.
|
|
|
|
# If invalid string is provided, it will raise +ActiveSupport::Duration::ISO8601Parser::ParsingError+.
|
|
|
|
def parse(iso8601duration)
|
|
|
|
parts = ISO8601Parser.new(iso8601duration).parse!
|
|
|
|
new(calculate_total_seconds(parts), parts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ===(other) #:nodoc:
|
|
|
|
other.is_a?(Duration)
|
|
|
|
rescue ::NoMethodError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def seconds(value) #:nodoc:
|
|
|
|
new(value, [[:seconds, value]])
|
|
|
|
end
|
|
|
|
|
|
|
|
def minutes(value) #:nodoc:
|
|
|
|
new(value * SECONDS_PER_MINUTE, [[:minutes, value]])
|
|
|
|
end
|
|
|
|
|
|
|
|
def hours(value) #:nodoc:
|
|
|
|
new(value * SECONDS_PER_HOUR, [[:hours, value]])
|
|
|
|
end
|
|
|
|
|
|
|
|
def days(value) #:nodoc:
|
|
|
|
new(value * SECONDS_PER_DAY, [[:days, value]])
|
|
|
|
end
|
|
|
|
|
|
|
|
def weeks(value) #:nodoc:
|
|
|
|
new(value * SECONDS_PER_WEEK, [[:weeks, value]])
|
|
|
|
end
|
|
|
|
|
|
|
|
def months(value) #:nodoc:
|
|
|
|
new(value * SECONDS_PER_MONTH, [[:months, value]])
|
|
|
|
end
|
|
|
|
|
|
|
|
def years(value) #:nodoc:
|
|
|
|
new(value * SECONDS_PER_YEAR, [[:years, value]])
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def calculate_total_seconds(parts)
|
|
|
|
parts.inject(0) do |total, (part, value)|
|
|
|
|
total + value * PARTS_IN_SECONDS[part]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
def initialize(value, parts) #:nodoc:
|
2016-11-29 13:02:14 -05:00
|
|
|
@value, @parts = value, parts.to_h
|
|
|
|
@parts.default = 0
|
2007-01-15 01:54:50 -05:00
|
|
|
end
|
2007-12-14 21:27:41 -05:00
|
|
|
|
Deprecate implicit coercion of `ActiveSupport::Duration`
Currently `ActiveSupport::Duration` implicitly converts to a seconds
value when used in a calculation except for the explicit examples of
addition and subtraction where the duration is the receiver, e.g:
>> 2 * 1.day
=> 172800
This results in lots of confusion especially when using durations
with dates because adding/subtracting a value from a date treats
integers as a day and not a second, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 2 * 1.day
=> Mon, 10 Apr 2490
To fix this we're implementing `coerce` so that we can provide a
deprecation warning with the intent of removing the implicit coercion
in Rails 5.2, e.g:
>> 2 * 1.day
DEPRECATION WARNING: Implicit coercion of ActiveSupport::Duration
to a Numeric is deprecated and will raise a TypeError in Rails 5.2.
=> 172800
In Rails 5.2 it will raise `TypeError`, e.g:
>> 2 * 1.day
TypeError: ActiveSupport::Duration can't be coerced into Integer
This is the same behavior as with other types in Ruby, e.g:
>> 2 * "foo"
TypeError: String can't be coerced into Integer
>> "foo" * 2
=> "foofoo"
As part of this deprecation add `*` and `/` methods to `AS::Duration`
so that calculations that keep the duration as the receiver work
correctly whether the final receiver is a `Date` or `Time`, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 1.day * 2
=> Fri, 03 Mar 2017
Fixes #27457.
2017-02-27 13:31:35 -05:00
|
|
|
def coerce(other) #:nodoc:
|
2017-03-15 10:38:21 -04:00
|
|
|
if Scalar === other
|
|
|
|
[other, self]
|
|
|
|
else
|
|
|
|
[Scalar.new(other), self]
|
|
|
|
end
|
Deprecate implicit coercion of `ActiveSupport::Duration`
Currently `ActiveSupport::Duration` implicitly converts to a seconds
value when used in a calculation except for the explicit examples of
addition and subtraction where the duration is the receiver, e.g:
>> 2 * 1.day
=> 172800
This results in lots of confusion especially when using durations
with dates because adding/subtracting a value from a date treats
integers as a day and not a second, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 2 * 1.day
=> Mon, 10 Apr 2490
To fix this we're implementing `coerce` so that we can provide a
deprecation warning with the intent of removing the implicit coercion
in Rails 5.2, e.g:
>> 2 * 1.day
DEPRECATION WARNING: Implicit coercion of ActiveSupport::Duration
to a Numeric is deprecated and will raise a TypeError in Rails 5.2.
=> 172800
In Rails 5.2 it will raise `TypeError`, e.g:
>> 2 * 1.day
TypeError: ActiveSupport::Duration can't be coerced into Integer
This is the same behavior as with other types in Ruby, e.g:
>> 2 * "foo"
TypeError: String can't be coerced into Integer
>> "foo" * 2
=> "foofoo"
As part of this deprecation add `*` and `/` methods to `AS::Duration`
so that calculations that keep the duration as the receiver work
correctly whether the final receiver is a `Date` or `Time`, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 1.day * 2
=> Fri, 03 Mar 2017
Fixes #27457.
2017-02-27 13:31:35 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Compares one Duration with another or a Numeric to this Duration.
|
|
|
|
# Numeric values are treated as seconds.
|
|
|
|
def <=>(other)
|
|
|
|
if Duration === other
|
|
|
|
value <=> other.value
|
|
|
|
elsif Numeric === other
|
|
|
|
value <=> other
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
# Adds another Duration or a Numeric to this Duration. Numeric values
|
|
|
|
# are treated as seconds.
|
|
|
|
def +(other)
|
|
|
|
if Duration === other
|
2016-11-29 13:02:14 -05:00
|
|
|
parts = @parts.dup
|
|
|
|
other.parts.each do |(key, value)|
|
|
|
|
parts[key] += value
|
|
|
|
end
|
|
|
|
Duration.new(value + other.value, parts)
|
2007-01-15 01:54:50 -05:00
|
|
|
else
|
2016-11-29 13:02:14 -05:00
|
|
|
seconds = @parts[:seconds] + other
|
|
|
|
Duration.new(value + other, @parts.merge(seconds: seconds))
|
2007-01-15 01:54:50 -05:00
|
|
|
end
|
|
|
|
end
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
# Subtracts another Duration or a Numeric from this Duration. Numeric
|
|
|
|
# values are treated as seconds.
|
|
|
|
def -(other)
|
|
|
|
self + (-other)
|
|
|
|
end
|
2007-12-14 21:27:41 -05:00
|
|
|
|
Deprecate implicit coercion of `ActiveSupport::Duration`
Currently `ActiveSupport::Duration` implicitly converts to a seconds
value when used in a calculation except for the explicit examples of
addition and subtraction where the duration is the receiver, e.g:
>> 2 * 1.day
=> 172800
This results in lots of confusion especially when using durations
with dates because adding/subtracting a value from a date treats
integers as a day and not a second, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 2 * 1.day
=> Mon, 10 Apr 2490
To fix this we're implementing `coerce` so that we can provide a
deprecation warning with the intent of removing the implicit coercion
in Rails 5.2, e.g:
>> 2 * 1.day
DEPRECATION WARNING: Implicit coercion of ActiveSupport::Duration
to a Numeric is deprecated and will raise a TypeError in Rails 5.2.
=> 172800
In Rails 5.2 it will raise `TypeError`, e.g:
>> 2 * 1.day
TypeError: ActiveSupport::Duration can't be coerced into Integer
This is the same behavior as with other types in Ruby, e.g:
>> 2 * "foo"
TypeError: String can't be coerced into Integer
>> "foo" * 2
=> "foofoo"
As part of this deprecation add `*` and `/` methods to `AS::Duration`
so that calculations that keep the duration as the receiver work
correctly whether the final receiver is a `Date` or `Time`, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 1.day * 2
=> Fri, 03 Mar 2017
Fixes #27457.
2017-02-27 13:31:35 -05:00
|
|
|
# Multiplies this Duration by a Numeric and returns a new Duration.
|
|
|
|
def *(other)
|
2017-03-15 10:38:21 -04:00
|
|
|
if Scalar === other || Duration === other
|
|
|
|
Duration.new(value * other.value, parts.map { |type, number| [type, number * other.value] })
|
|
|
|
elsif Numeric === other
|
Deprecate implicit coercion of `ActiveSupport::Duration`
Currently `ActiveSupport::Duration` implicitly converts to a seconds
value when used in a calculation except for the explicit examples of
addition and subtraction where the duration is the receiver, e.g:
>> 2 * 1.day
=> 172800
This results in lots of confusion especially when using durations
with dates because adding/subtracting a value from a date treats
integers as a day and not a second, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 2 * 1.day
=> Mon, 10 Apr 2490
To fix this we're implementing `coerce` so that we can provide a
deprecation warning with the intent of removing the implicit coercion
in Rails 5.2, e.g:
>> 2 * 1.day
DEPRECATION WARNING: Implicit coercion of ActiveSupport::Duration
to a Numeric is deprecated and will raise a TypeError in Rails 5.2.
=> 172800
In Rails 5.2 it will raise `TypeError`, e.g:
>> 2 * 1.day
TypeError: ActiveSupport::Duration can't be coerced into Integer
This is the same behavior as with other types in Ruby, e.g:
>> 2 * "foo"
TypeError: String can't be coerced into Integer
>> "foo" * 2
=> "foofoo"
As part of this deprecation add `*` and `/` methods to `AS::Duration`
so that calculations that keep the duration as the receiver work
correctly whether the final receiver is a `Date` or `Time`, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 1.day * 2
=> Fri, 03 Mar 2017
Fixes #27457.
2017-02-27 13:31:35 -05:00
|
|
|
Duration.new(value * other, parts.map { |type, number| [type, number * other] })
|
|
|
|
else
|
2017-03-15 10:38:21 -04:00
|
|
|
raise_type_error(other)
|
Deprecate implicit coercion of `ActiveSupport::Duration`
Currently `ActiveSupport::Duration` implicitly converts to a seconds
value when used in a calculation except for the explicit examples of
addition and subtraction where the duration is the receiver, e.g:
>> 2 * 1.day
=> 172800
This results in lots of confusion especially when using durations
with dates because adding/subtracting a value from a date treats
integers as a day and not a second, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 2 * 1.day
=> Mon, 10 Apr 2490
To fix this we're implementing `coerce` so that we can provide a
deprecation warning with the intent of removing the implicit coercion
in Rails 5.2, e.g:
>> 2 * 1.day
DEPRECATION WARNING: Implicit coercion of ActiveSupport::Duration
to a Numeric is deprecated and will raise a TypeError in Rails 5.2.
=> 172800
In Rails 5.2 it will raise `TypeError`, e.g:
>> 2 * 1.day
TypeError: ActiveSupport::Duration can't be coerced into Integer
This is the same behavior as with other types in Ruby, e.g:
>> 2 * "foo"
TypeError: String can't be coerced into Integer
>> "foo" * 2
=> "foofoo"
As part of this deprecation add `*` and `/` methods to `AS::Duration`
so that calculations that keep the duration as the receiver work
correctly whether the final receiver is a `Date` or `Time`, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 1.day * 2
=> Fri, 03 Mar 2017
Fixes #27457.
2017-02-27 13:31:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-03 08:50:48 -05:00
|
|
|
# Divides this Duration by a Numeric and returns a new Duration.
|
Deprecate implicit coercion of `ActiveSupport::Duration`
Currently `ActiveSupport::Duration` implicitly converts to a seconds
value when used in a calculation except for the explicit examples of
addition and subtraction where the duration is the receiver, e.g:
>> 2 * 1.day
=> 172800
This results in lots of confusion especially when using durations
with dates because adding/subtracting a value from a date treats
integers as a day and not a second, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 2 * 1.day
=> Mon, 10 Apr 2490
To fix this we're implementing `coerce` so that we can provide a
deprecation warning with the intent of removing the implicit coercion
in Rails 5.2, e.g:
>> 2 * 1.day
DEPRECATION WARNING: Implicit coercion of ActiveSupport::Duration
to a Numeric is deprecated and will raise a TypeError in Rails 5.2.
=> 172800
In Rails 5.2 it will raise `TypeError`, e.g:
>> 2 * 1.day
TypeError: ActiveSupport::Duration can't be coerced into Integer
This is the same behavior as with other types in Ruby, e.g:
>> 2 * "foo"
TypeError: String can't be coerced into Integer
>> "foo" * 2
=> "foofoo"
As part of this deprecation add `*` and `/` methods to `AS::Duration`
so that calculations that keep the duration as the receiver work
correctly whether the final receiver is a `Date` or `Time`, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 1.day * 2
=> Fri, 03 Mar 2017
Fixes #27457.
2017-02-27 13:31:35 -05:00
|
|
|
def /(other)
|
2017-03-15 10:38:21 -04:00
|
|
|
if Scalar === other || Duration === other
|
|
|
|
Duration.new(value / other.value, parts.map { |type, number| [type, number / other.value] })
|
|
|
|
elsif Numeric === other
|
Deprecate implicit coercion of `ActiveSupport::Duration`
Currently `ActiveSupport::Duration` implicitly converts to a seconds
value when used in a calculation except for the explicit examples of
addition and subtraction where the duration is the receiver, e.g:
>> 2 * 1.day
=> 172800
This results in lots of confusion especially when using durations
with dates because adding/subtracting a value from a date treats
integers as a day and not a second, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 2 * 1.day
=> Mon, 10 Apr 2490
To fix this we're implementing `coerce` so that we can provide a
deprecation warning with the intent of removing the implicit coercion
in Rails 5.2, e.g:
>> 2 * 1.day
DEPRECATION WARNING: Implicit coercion of ActiveSupport::Duration
to a Numeric is deprecated and will raise a TypeError in Rails 5.2.
=> 172800
In Rails 5.2 it will raise `TypeError`, e.g:
>> 2 * 1.day
TypeError: ActiveSupport::Duration can't be coerced into Integer
This is the same behavior as with other types in Ruby, e.g:
>> 2 * "foo"
TypeError: String can't be coerced into Integer
>> "foo" * 2
=> "foofoo"
As part of this deprecation add `*` and `/` methods to `AS::Duration`
so that calculations that keep the duration as the receiver work
correctly whether the final receiver is a `Date` or `Time`, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 1.day * 2
=> Fri, 03 Mar 2017
Fixes #27457.
2017-02-27 13:31:35 -05:00
|
|
|
Duration.new(value / other, parts.map { |type, number| [type, number / other] })
|
|
|
|
else
|
2017-03-15 10:38:21 -04:00
|
|
|
raise_type_error(other)
|
Deprecate implicit coercion of `ActiveSupport::Duration`
Currently `ActiveSupport::Duration` implicitly converts to a seconds
value when used in a calculation except for the explicit examples of
addition and subtraction where the duration is the receiver, e.g:
>> 2 * 1.day
=> 172800
This results in lots of confusion especially when using durations
with dates because adding/subtracting a value from a date treats
integers as a day and not a second, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 2 * 1.day
=> Mon, 10 Apr 2490
To fix this we're implementing `coerce` so that we can provide a
deprecation warning with the intent of removing the implicit coercion
in Rails 5.2, e.g:
>> 2 * 1.day
DEPRECATION WARNING: Implicit coercion of ActiveSupport::Duration
to a Numeric is deprecated and will raise a TypeError in Rails 5.2.
=> 172800
In Rails 5.2 it will raise `TypeError`, e.g:
>> 2 * 1.day
TypeError: ActiveSupport::Duration can't be coerced into Integer
This is the same behavior as with other types in Ruby, e.g:
>> 2 * "foo"
TypeError: String can't be coerced into Integer
>> "foo" * 2
=> "foofoo"
As part of this deprecation add `*` and `/` methods to `AS::Duration`
so that calculations that keep the duration as the receiver work
correctly whether the final receiver is a `Date` or `Time`, e.g:
>> Date.today
=> Wed, 01 Mar 2017
>> Date.today + 1.day * 2
=> Fri, 03 Mar 2017
Fixes #27457.
2017-02-27 13:31:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
def -@ #:nodoc:
|
2016-10-28 23:05:58 -04:00
|
|
|
Duration.new(-value, parts.map { |type, number| [type, -number] })
|
2007-01-15 01:54:50 -05:00
|
|
|
end
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
def is_a?(klass) #:nodoc:
|
2014-08-23 07:09:41 -04:00
|
|
|
Duration == klass || value.is_a?(klass)
|
2007-01-15 01:54:50 -05:00
|
|
|
end
|
2010-05-19 23:43:39 -04:00
|
|
|
alias :kind_of? :is_a?
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2014-08-17 10:31:00 -04:00
|
|
|
def instance_of?(klass) # :nodoc:
|
2014-08-23 07:09:41 -04:00
|
|
|
Duration == klass || value.instance_of?(klass)
|
2014-08-17 10:31:00 -04:00
|
|
|
end
|
|
|
|
|
2012-09-17 01:22:18 -04:00
|
|
|
# Returns +true+ if +other+ is also a Duration instance with the
|
|
|
|
# same +value+, or if <tt>other == value</tt>.
|
2007-12-14 21:27:41 -05:00
|
|
|
def ==(other)
|
|
|
|
if Duration === other
|
|
|
|
other.value == value
|
|
|
|
else
|
|
|
|
other == value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-25 06:48:29 -04:00
|
|
|
# Returns the amount of seconds a duration covers as a string.
|
|
|
|
# For more information check to_i method.
|
|
|
|
#
|
|
|
|
# 1.day.to_s # => "86400"
|
2014-08-20 02:56:54 -04:00
|
|
|
def to_s
|
|
|
|
@value.to_s
|
|
|
|
end
|
|
|
|
|
2015-03-23 07:14:37 -04:00
|
|
|
# Returns the number of seconds that this Duration represents.
|
|
|
|
#
|
|
|
|
# 1.minute.to_i # => 60
|
|
|
|
# 1.hour.to_i # => 3600
|
|
|
|
# 1.day.to_i # => 86400
|
|
|
|
#
|
|
|
|
# Note that this conversion makes some assumptions about the
|
2017-01-08 17:43:49 -05:00
|
|
|
# duration of some periods, e.g. months are always 1/12 of year
|
|
|
|
# and years are 365.2425 days:
|
2015-03-23 07:14:37 -04:00
|
|
|
#
|
2017-01-08 17:43:49 -05:00
|
|
|
# # equivalent to (1.year / 12).to_i
|
|
|
|
# 1.month.to_i # => 2629746
|
2015-03-23 07:14:37 -04:00
|
|
|
#
|
2017-01-08 17:43:49 -05:00
|
|
|
# # equivalent to 365.2425.days.to_i
|
|
|
|
# 1.year.to_i # => 31556952
|
2015-03-23 07:14:37 -04:00
|
|
|
#
|
|
|
|
# In such cases, Ruby's core
|
|
|
|
# Date[http://ruby-doc.org/stdlib/libdoc/date/rdoc/Date.html] and
|
|
|
|
# Time[http://ruby-doc.org/stdlib/libdoc/time/rdoc/Time.html] should be used for precision
|
|
|
|
# date and time arithmetic.
|
|
|
|
def to_i
|
|
|
|
@value.to_i
|
|
|
|
end
|
|
|
|
|
2013-08-07 09:18:30 -04:00
|
|
|
# Returns +true+ if +other+ is also a Duration instance, which has the
|
|
|
|
# same parts as this one.
|
2014-04-08 19:24:35 -04:00
|
|
|
def eql?(other)
|
2013-08-07 09:18:30 -04:00
|
|
|
Duration === other && other.value.eql?(value)
|
2014-04-08 19:24:35 -04:00
|
|
|
end
|
|
|
|
|
2014-10-03 05:53:54 -04:00
|
|
|
def hash
|
|
|
|
@value.hash
|
|
|
|
end
|
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
# Calculates a new Time or Date that is as far in the future
|
|
|
|
# as this Duration represents.
|
2008-04-20 22:57:04 -04:00
|
|
|
def since(time = ::Time.current)
|
2007-01-15 01:54:50 -05:00
|
|
|
sum(1, time)
|
|
|
|
end
|
|
|
|
alias :from_now :since
|
2017-02-22 02:45:48 -05:00
|
|
|
alias :after :since
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
# Calculates a new Time or Date that is as far in the past
|
|
|
|
# as this Duration represents.
|
2008-04-20 22:57:04 -04:00
|
|
|
def ago(time = ::Time.current)
|
2007-01-15 01:54:50 -05:00
|
|
|
sum(-1, time)
|
|
|
|
end
|
|
|
|
alias :until :ago
|
2017-02-22 02:45:48 -05:00
|
|
|
alias :before :ago
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
def inspect #:nodoc:
|
2013-08-27 23:07:18 -04:00
|
|
|
parts.
|
2016-10-28 23:05:58 -04:00
|
|
|
reduce(::Hash.new(0)) { |h, (l, r)| h[l] += r; h }.
|
2016-08-16 03:30:11 -04:00
|
|
|
sort_by { |unit, _ | [:years, :months, :weeks, :days, :hours, :minutes, :seconds].index(unit) }.
|
|
|
|
map { |unit, val| "#{val} #{val == 1 ? unit.to_s.chop : unit.to_s}" }.
|
2015-01-12 13:34:32 -05:00
|
|
|
to_sentence(locale: ::I18n.default_locale)
|
2007-01-15 01:54:50 -05:00
|
|
|
end
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2010-12-29 17:20:14 -05:00
|
|
|
def as_json(options = nil) #:nodoc:
|
|
|
|
to_i
|
|
|
|
end
|
|
|
|
|
2014-09-14 09:16:51 -04:00
|
|
|
# Build ISO 8601 Duration string for this duration.
|
|
|
|
# The +precision+ parameter can be used to limit seconds' precision of duration.
|
|
|
|
def iso8601(precision: nil)
|
|
|
|
ISO8601Serializer.new(self, precision: precision).serialize
|
|
|
|
end
|
|
|
|
|
2016-12-17 03:13:50 -05:00
|
|
|
private
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2016-12-17 03:13:50 -05:00
|
|
|
def sum(sign, time = ::Time.current)
|
2016-10-28 23:05:58 -04:00
|
|
|
parts.inject(time) do |t, (type, number)|
|
2007-12-14 21:27:41 -05:00
|
|
|
if t.acts_like?(:time) || t.acts_like?(:date)
|
|
|
|
if type == :seconds
|
|
|
|
t.since(sign * number)
|
2015-12-27 11:32:31 -05:00
|
|
|
elsif type == :minutes
|
|
|
|
t.since(sign * number * 60)
|
|
|
|
elsif type == :hours
|
|
|
|
t.since(sign * number * 3600)
|
2007-12-14 21:27:41 -05:00
|
|
|
else
|
|
|
|
t.advance(type => sign * number)
|
|
|
|
end
|
2007-01-15 01:54:50 -05:00
|
|
|
else
|
2008-01-02 04:08:14 -05:00
|
|
|
raise ::ArgumentError, "expected a time or date, got #{time.inspect}"
|
2007-01-15 01:54:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2017-04-22 05:29:05 -04:00
|
|
|
def respond_to_missing?(method, _)
|
|
|
|
value.respond_to?(method)
|
|
|
|
end
|
|
|
|
|
2016-12-23 21:33:04 -05:00
|
|
|
def method_missing(method, *args, &block)
|
2017-04-22 05:29:05 -04:00
|
|
|
value.public_send(method, *args, &block)
|
2007-12-14 21:27:41 -05:00
|
|
|
end
|
2017-03-15 10:38:21 -04:00
|
|
|
|
|
|
|
def raise_type_error(other)
|
|
|
|
raise TypeError, "no implicit conversion of #{other.class} into #{self.class}"
|
|
|
|
end
|
2007-01-15 01:54:50 -05:00
|
|
|
end
|
2007-10-06 21:07:00 -04:00
|
|
|
end
|