2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2017-10-21 09:11:29 -04:00
|
|
|
require "active_support/core_ext/array/conversions"
|
|
|
|
require "active_support/core_ext/module/delegation"
|
|
|
|
require "active_support/core_ext/object/acts_like"
|
|
|
|
require "active_support/core_ext/string/filters"
|
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
|
2019-12-20 03:06:03 -05:00
|
|
|
seconds = value + other.parts.fetch(:seconds, 0)
|
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
|
|
|
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
|
2019-12-20 03:06:03 -05:00
|
|
|
seconds = value - other.parts.fetch(:seconds, 0)
|
|
|
|
new_parts = other.parts.transform_values(&:-@)
|
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
|
|
|
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
|
2019-12-20 03:06:03 -05:00
|
|
|
new_parts = other.parts.transform_values { |other_value| value * other_value }
|
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
|
|
|
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
|
2017-07-27 07:30:17 -04:00
|
|
|
value / other.value
|
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
|
|
|
else
|
|
|
|
calculate(:/, other)
|
|
|
|
end
|
2017-03-15 10:38:21 -04:00
|
|
|
end
|
|
|
|
|
2017-06-28 10:08:02 -04:00
|
|
|
def %(other)
|
|
|
|
if Duration === other
|
|
|
|
Duration.build(value % other.value)
|
|
|
|
else
|
|
|
|
calculate(:%, other)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-15 10:38:21 -04:00
|
|
|
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
|
|
|
|
2017-06-28 10:08:02 -04:00
|
|
|
PARTS = [:years, :months, :weeks, :days, :hours, :minutes, :seconds].freeze
|
|
|
|
|
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.
|
|
|
|
#
|
2017-08-21 20:16:50 -04:00
|
|
|
# See {ISO 8601}[https://en.wikipedia.org/wiki/ISO_8601#Durations] for more information.
|
2017-01-12 05:02:16 -05:00
|
|
|
# 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:
|
2019-12-20 03:06:03 -05:00
|
|
|
new(value, seconds: value)
|
2017-01-12 05:02:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def minutes(value) #:nodoc:
|
2019-12-20 03:06:03 -05:00
|
|
|
new(value * SECONDS_PER_MINUTE, minutes: value)
|
2017-01-12 05:02:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def hours(value) #:nodoc:
|
2019-12-20 03:06:03 -05:00
|
|
|
new(value * SECONDS_PER_HOUR, hours: value)
|
2017-01-12 05:02:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def days(value) #:nodoc:
|
2019-12-20 03:06:03 -05:00
|
|
|
new(value * SECONDS_PER_DAY, days: value)
|
2017-01-12 05:02:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def weeks(value) #:nodoc:
|
2019-12-20 03:06:03 -05:00
|
|
|
new(value * SECONDS_PER_WEEK, weeks: value)
|
2017-01-12 05:02:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def months(value) #:nodoc:
|
2019-12-20 03:06:03 -05:00
|
|
|
new(value * SECONDS_PER_MONTH, months: value)
|
2017-01-12 05:02:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def years(value) #:nodoc:
|
2019-12-20 03:06:03 -05:00
|
|
|
new(value * SECONDS_PER_YEAR, years: value)
|
2017-01-12 05:02:16 -05:00
|
|
|
end
|
|
|
|
|
2017-06-28 10:08:02 -04:00
|
|
|
# Creates a new Duration from a seconds value that is converted
|
|
|
|
# to the individual parts:
|
|
|
|
#
|
|
|
|
# ActiveSupport::Duration.build(31556952).parts # => {:years=>1}
|
|
|
|
# ActiveSupport::Duration.build(2716146).parts # => {:months=>1, :days=>1}
|
|
|
|
#
|
|
|
|
def build(value)
|
2019-08-22 11:11:42 -04:00
|
|
|
unless value.is_a?(::Numeric)
|
|
|
|
raise TypeError, "can't build an #{self.name} from a #{value.class.name}"
|
|
|
|
end
|
|
|
|
|
2017-06-28 10:08:02 -04:00
|
|
|
parts = {}
|
2019-12-04 08:48:25 -05:00
|
|
|
remainder = value.round(9)
|
2017-06-28 10:08:02 -04:00
|
|
|
|
|
|
|
PARTS.each do |part|
|
|
|
|
unless part == :seconds
|
|
|
|
part_in_seconds = PARTS_IN_SECONDS[part]
|
|
|
|
parts[part] = remainder.div(part_in_seconds)
|
2019-12-04 08:48:25 -05:00
|
|
|
remainder %= part_in_seconds
|
2017-06-28 10:08:02 -04:00
|
|
|
end
|
2019-12-04 08:48:25 -05:00
|
|
|
end unless value == 0
|
2017-06-28 10:08:02 -04:00
|
|
|
|
|
|
|
parts[:seconds] = remainder
|
|
|
|
|
|
|
|
new(value, parts)
|
|
|
|
end
|
|
|
|
|
2017-01-12 05:02:16 -05:00
|
|
|
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:
|
2019-12-20 03:06:03 -05:00
|
|
|
@value, @parts = value, parts
|
2019-12-04 08:48:25 -05:00
|
|
|
@parts.reject! { |k, v| v.zero? } unless value == 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:
|
2019-01-08 17:36:00 -05:00
|
|
|
case other
|
|
|
|
when Scalar
|
2017-03-15 10:38:21 -04:00
|
|
|
[other, self]
|
2019-01-08 17:36:00 -05:00
|
|
|
when Duration
|
|
|
|
[Scalar.new(other.value), self]
|
2017-03-15 10:38:21 -04:00
|
|
|
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
|
2019-12-20 03:06:03 -05:00
|
|
|
parts = @parts.merge(other.parts) do |_key, value, other_value|
|
|
|
|
value + other_value
|
2016-11-29 13:02:14 -05:00
|
|
|
end
|
|
|
|
Duration.new(value + other.value, parts)
|
2007-01-15 01:54:50 -05:00
|
|
|
else
|
2019-12-20 03:06:03 -05:00
|
|
|
seconds = @parts.fetch(:seconds, 0) + other
|
2016-11-29 13:02:14 -05:00
|
|
|
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
|
2019-12-20 03:06:03 -05:00
|
|
|
Duration.new(value * other.value, parts.transform_values { |number| number * other.value })
|
2017-03-15 10:38:21 -04:00
|
|
|
elsif Numeric === other
|
2019-12-20 03:06:03 -05:00
|
|
|
Duration.new(value * other, parts.transform_values { |number| number * 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
|
|
|
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-07-27 07:30:17 -04:00
|
|
|
if Scalar === other
|
2019-12-20 03:06:03 -05:00
|
|
|
Duration.new(value / other.value, parts.transform_values { |number| number / other.value })
|
2017-07-27 07:30:17 -04:00
|
|
|
elsif Duration === other
|
|
|
|
value / other.value
|
2017-03-15 10:38:21 -04:00
|
|
|
elsif Numeric === other
|
2019-12-20 03:06:03 -05:00
|
|
|
Duration.new(value / other, parts.transform_values { |number| number / 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
|
|
|
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-06-28 10:08:02 -04:00
|
|
|
# Returns the modulo of this Duration by another Duration or Numeric.
|
|
|
|
# Numeric values are treated as seconds.
|
|
|
|
def %(other)
|
|
|
|
if Duration === other || Scalar === other
|
|
|
|
Duration.build(value % other.value)
|
|
|
|
elsif Numeric === other
|
|
|
|
Duration.build(value % other)
|
|
|
|
else
|
|
|
|
raise_type_error(other)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
def -@ #:nodoc:
|
2019-12-20 03:06:03 -05:00
|
|
|
Duration.new(-value, parts.transform_values(&:-@))
|
2007-01-15 01:54:50 -05:00
|
|
|
end
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2020-04-29 19:35:05 -04:00
|
|
|
def +@ #:nodoc:
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
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
|
2019-10-02 05:54:57 -04:00
|
|
|
# Date[https://ruby-doc.org/stdlib/libdoc/date/rdoc/Date.html] and
|
|
|
|
# Time[https://ruby-doc.org/stdlib/libdoc/time/rdoc/Time.html] should be used for precision
|
2015-03-23 07:14:37 -04:00
|
|
|
# date and time arithmetic.
|
|
|
|
def to_i
|
|
|
|
@value.to_i
|
|
|
|
end
|
2020-03-11 10:34:38 -04:00
|
|
|
alias :to_seconds :to_i
|
|
|
|
|
|
|
|
# Returns the amount of minutes a duration covers as a float
|
|
|
|
#
|
|
|
|
# 1.day.to_minutes # => 1440.0
|
|
|
|
def to_minutes
|
|
|
|
to_seconds / SECONDS_PER_MINUTE.to_f
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the amount of hours a duration covers as a float
|
|
|
|
#
|
|
|
|
# 1.day.to_hours # => 24.0
|
|
|
|
def to_hours
|
|
|
|
to_seconds / SECONDS_PER_HOUR.to_f
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the amount of days a duration covers as a float
|
|
|
|
#
|
|
|
|
# 12.hours.to_days # => 0.5
|
|
|
|
def to_days
|
|
|
|
to_seconds / SECONDS_PER_DAY.to_f
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the amount of weeks a duration covers as a float
|
|
|
|
#
|
|
|
|
# 2.months.to_weeks # => 8.696
|
|
|
|
def to_weeks
|
|
|
|
to_seconds / SECONDS_PER_WEEK.to_f
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the amount of months a duration covers as a float
|
|
|
|
#
|
|
|
|
# 9.weeks.to_months # => 2.07
|
|
|
|
def to_months
|
|
|
|
to_seconds / SECONDS_PER_MONTH.to_f
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the amount of years a duration covers as a float
|
|
|
|
#
|
|
|
|
# 30.days.to_years # => 0.082
|
|
|
|
def to_years
|
|
|
|
to_seconds / SECONDS_PER_YEAR.to_f
|
|
|
|
end
|
2015-03-23 07:14:37 -04:00
|
|
|
|
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:
|
2019-11-30 02:32:57 -05:00
|
|
|
return "#{value} seconds" if parts.empty?
|
2017-12-13 03:16:46 -05:00
|
|
|
|
2013-08-27 23:07:18 -04:00
|
|
|
parts.
|
2017-06-28 10:08:02 -04:00
|
|
|
sort_by { |unit, _ | PARTS.index(unit) }.
|
2016-08-16 03:30:11 -04:00
|
|
|
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
|
|
|
|
|
2018-02-07 08:06:11 -05:00
|
|
|
def init_with(coder) #:nodoc:
|
|
|
|
initialize(coder["value"], coder["parts"])
|
|
|
|
end
|
|
|
|
|
|
|
|
def encode_with(coder) #:nodoc:
|
|
|
|
coder.map = { "value" => @value, "parts" => @parts }
|
|
|
|
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
|
|
|
|
def sum(sign, time = ::Time.current)
|
2019-12-02 03:58:00 -05:00
|
|
|
unless time.acts_like?(:time) || time.acts_like?(:date)
|
|
|
|
raise ::ArgumentError, "expected a time or date, got #{time.inspect}"
|
|
|
|
end
|
|
|
|
|
|
|
|
if parts.empty?
|
|
|
|
time.since(sign * value)
|
|
|
|
else
|
|
|
|
parts.inject(time) do |t, (type, number)|
|
2007-12-14 21:27:41 -05:00
|
|
|
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
|
|
|
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
|