class ActiveSupport::Duration

1.month.ago # equivalent to Time.now.advance(months: -1)
Time#advance, respectively. It mainly supports the methods on Numeric.
Provides accurate date and time measurements using Date#advance and

def %(other)

Numeric values are treated as seconds.
Returns the modulo of this Duration by another Duration or Numeric.
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

def *(other)

Multiplies this Duration by a Numeric and returns a new Duration.
def *(other)
  if Scalar === other || Duration === other
    Duration.new(value * other.value, parts.map { |type, number| [type, number * other.value] })
  elsif Numeric === other
    Duration.new(value * other, parts.map { |type, number| [type, number * other] })
  else
    raise_type_error(other)
  end
end

def +(other)

are treated as seconds.
Adds another Duration or a Numeric to this Duration. Numeric values
def +(other)
  if Duration === other
    parts = @parts.dup
    other.parts.each do |(key, value)|
      parts[key] += value
    end
    Duration.new(value + other.value, parts)
  else
    seconds = @parts[:seconds] + other
    Duration.new(value + other, @parts.merge(seconds: seconds))
  end
end

def -(other)

values are treated as seconds.
Subtracts another Duration or a Numeric from this Duration. Numeric
def -(other)
  self + (-other)
end

def -@ #:nodoc:

:nodoc:
def -@ #:nodoc:
  Duration.new(-value, parts.map { |type, number| [type, -number] })
end

def /(other)

Divides this Duration by a Numeric and returns a new Duration.
def /(other)
  if Scalar === other
    Duration.new(value / other.value, parts.map { |type, number| [type, number / other.value] })
  elsif Duration === other
    value / other.value
  elsif Numeric === other
    Duration.new(value / other, parts.map { |type, number| [type, number / other] })
  else
    raise_type_error(other)
  end
end

def <=>(other)

Numeric values are treated as seconds.
Compares one Duration with another or a Numeric to this Duration.
def <=>(other)
  if Duration === other
    value <=> other.value
  elsif Numeric === other
    value <=> other
  end
end

def ==(other)

same +value+, or if other == value.
Returns +true+ if +other+ is also a Duration instance with the
def ==(other)
  if Duration === other
    other.value == value
  else
    other == value
  end
end

def ===(other) #:nodoc:

:nodoc:
def ===(other) #:nodoc:
  other.is_a?(Duration)
rescue ::NoMethodError
  false
end

def ago(time = ::Time.current)

as this Duration represents.
Calculates a new Time or Date that is as far in the past
def ago(time = ::Time.current)
  sum(-1, time)
end

def as_json(options = nil) #:nodoc:

:nodoc:
def as_json(options = nil) #:nodoc:
  to_i
end

def build(value)


ActiveSupport::Duration.build(2716146).parts # => {:months=>1, :days=>1}
ActiveSupport::Duration.build(31556952).parts # => {:years=>1}

to the individual parts:
Creates a new Duration from a seconds value that is converted
def build(value)
  parts = {}
  remainder = value.to_f
  PARTS.each do |part|
    unless part == :seconds
      part_in_seconds = PARTS_IN_SECONDS[part]
      parts[part] = remainder.div(part_in_seconds)
      remainder = (remainder % part_in_seconds).round(9)
    end
  end
  parts[:seconds] = remainder
  new(value, parts)
end

def calculate_total_seconds(parts)

def calculate_total_seconds(parts)
  parts.inject(0) do |total, (part, value)|
    total + value * PARTS_IN_SECONDS[part]
  end
end

def coerce(other) #:nodoc:

:nodoc:
def coerce(other) #:nodoc:
  case other
  when Scalar
    [other, self]
  when Duration
    [Scalar.new(other.value), self]
  else
    [Scalar.new(other), self]
  end
end

def days(value) #:nodoc:

:nodoc:
def days(value) #:nodoc:
  new(value * SECONDS_PER_DAY, [[:days, value]])
end

def encode_with(coder) #:nodoc:

:nodoc:
def encode_with(coder) #:nodoc:
  coder.map = { "value" => @value, "parts" => @parts }
end

def eql?(other)

same parts as this one.
Returns +true+ if +other+ is also a Duration instance, which has the
def eql?(other)
  Duration === other && other.value.eql?(value)
end

def hash

def hash
  @value.hash
end

def hours(value) #:nodoc:

:nodoc:
def hours(value) #:nodoc:
  new(value * SECONDS_PER_HOUR, [[:hours, value]])
end

def init_with(coder) #:nodoc:

:nodoc:
def init_with(coder) #:nodoc:
  initialize(coder["value"], coder["parts"])
end

def initialize(value, parts) #:nodoc:

:nodoc:
def initialize(value, parts) #:nodoc:
  @value, @parts = value, parts.to_h
  @parts.default = 0
  @parts.reject! { |k, v| v.zero? }
end

def inspect #:nodoc:

:nodoc:
def inspect #:nodoc:
  return "0 seconds" if parts.empty?
  parts.
    sort_by { |unit,  _ | PARTS.index(unit) }.
    map     { |unit, val| "#{val} #{val == 1 ? unit.to_s.chop : unit.to_s}" }.
    to_sentence(locale: ::I18n.default_locale)
end

def instance_of?(klass) # :nodoc:

:nodoc:
def instance_of?(klass) # :nodoc:
  Duration == klass || value.instance_of?(klass)
end

def is_a?(klass) #:nodoc:

:nodoc:
def is_a?(klass) #:nodoc:
  Duration == klass || value.is_a?(klass)
end

def iso8601(precision: nil)

The +precision+ parameter can be used to limit seconds' precision of duration.
Build ISO 8601 Duration string for this duration.
def iso8601(precision: nil)
  ISO8601Serializer.new(self, precision: precision).serialize
end

def method_missing(method, *args, &block)

def method_missing(method, *args, &block)
  value.public_send(method, *args, &block)
end

def minutes(value) #:nodoc:

:nodoc:
def minutes(value) #:nodoc:
  new(value * SECONDS_PER_MINUTE, [[:minutes, value]])
end

def months(value) #:nodoc:

:nodoc:
def months(value) #:nodoc:
  new(value * SECONDS_PER_MONTH, [[:months, value]])
end

def parse(iso8601duration)

If invalid string is provided, it will raise +ActiveSupport::Duration::ISO8601Parser::ParsingError+.
This method allows negative parts to be present in pattern.
See {ISO 8601}[https://en.wikipedia.org/wiki/ISO_8601#Durations] for more information.

Creates a new Duration from string formatted according to ISO 8601 Duration.
def parse(iso8601duration)
  parts = ISO8601Parser.new(iso8601duration).parse!
  new(calculate_total_seconds(parts), parts)
end

def raise_type_error(other)

def raise_type_error(other)
  raise TypeError, "no implicit conversion of #{other.class} into #{self.class}"
end

def respond_to_missing?(method, _)

def respond_to_missing?(method, _)
  value.respond_to?(method)
end

def seconds(value) #:nodoc:

:nodoc:
def seconds(value) #:nodoc:
  new(value, [[:seconds, value]])
end

def since(time = ::Time.current)

as this Duration represents.
Calculates a new Time or Date that is as far in the future
def since(time = ::Time.current)
  sum(1, time)
end

def sum(sign, time = ::Time.current)

def sum(sign, time = ::Time.current)
  parts.inject(time) do |t, (type, number)|
    if t.acts_like?(:time) || t.acts_like?(:date)
      if type == :seconds
        t.since(sign * number)
      elsif type == :minutes
        t.since(sign * number * 60)
      elsif type == :hours
        t.since(sign * number * 3600)
      else
        t.advance(type => sign * number)
      end
    else
      raise ::ArgumentError, "expected a time or date, got #{time.inspect}"
    end
  end
end

def to_i

date and time arithmetic.
Time[https://ruby-doc.org/stdlib/libdoc/time/rdoc/Time.html] should be used for precision
Date[https://ruby-doc.org/stdlib/libdoc/date/rdoc/Date.html] and
In such cases, Ruby's core

1.year.to_i # => 31556952
# equivalent to 365.2425.days.to_i

1.month.to_i # => 2629746
# equivalent to (1.year / 12).to_i

and years are 365.2425 days:
duration of some periods, e.g. months are always 1/12 of year
Note that this conversion makes some assumptions about the

1.day.to_i # => 86400
1.hour.to_i # => 3600
1.minute.to_i # => 60

Returns the number of seconds that this Duration represents.
def to_i
  @value.to_i
end

def to_s

1.day.to_s # => "86400"

For more information check to_i method.
Returns the amount of seconds a duration covers as a string.
def to_s
  @value.to_s
end

def weeks(value) #:nodoc:

:nodoc:
def weeks(value) #:nodoc:
  new(value * SECONDS_PER_WEEK, [[:weeks, value]])
end

def years(value) #:nodoc:

:nodoc:
def years(value) #:nodoc:
  new(value * SECONDS_PER_YEAR, [[:years, value]])
end