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 self.===(other) #:nodoc:
def self.===(other) #:nodoc: other.is_a?(Duration) rescue ::NoMethodError false end
def +(other)
Adds another Duration or a Numeric to this Duration. Numeric values
def +(other) if Duration === other Duration.new(value + other.value, @parts + other.parts) else Duration.new(value + other, @parts + [[:seconds, other]]) end end
def -(other)
Subtracts another Duration or a Numeric from this Duration. Numeric
def -(other) self + (-other) end
def -@ #:nodoc:
def -@ #:nodoc: Duration.new(-value, parts.map { |type,number| [type, -number] }) end
def ==(other)
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:
Remove it when we drop support for 2.0.0-p353.
For more information, check rails/rails#13055.
We define it as a workaround to Ruby 2.0.0-p353 bug.
def ===(other) #:nodoc: value === other end
def ago(time = ::Time.current)
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:
def as_json(options = nil) #:nodoc: to_i end
def eql?(other)
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 initialize(value, parts) #:nodoc:
def initialize(value, parts) #:nodoc: @value, @parts = value, parts end
def inspect #:nodoc:
def inspect #:nodoc: parts. reduce(::Hash.new(0)) { |h,(l,r)| h[l] += r; h }. sort_by {|unit, _ | [:years, :months, :days, :minutes, :seconds].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:
def instance_of?(klass) # :nodoc: Duration == klass || value.instance_of?(klass) end
def is_a?(klass) #:nodoc:
def is_a?(klass) #:nodoc: Duration == klass || value.is_a?(klass) end
def method_missing(method, *args, &block) #:nodoc:
def method_missing(method, *args, &block) #:nodoc: value.send(method, *args, &block) end
def respond_to_missing?(method, include_private=false) #:nodoc
def respond_to_missing?(method, include_private=false) #:nodoc @value.respond_to?(method, include_private) end
def since(time = ::Time.current)
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) #:nodoc:
def sum(sign, time = ::Time.current) #:nodoc: parts.inject(time) do |t,(type,number)| if t.acts_like?(:time) || t.acts_like?(:date) if type == :seconds t.since(sign * number) else t.advance(type => sign * number) end else raise ::ArgumentError, "expected a time or date, got #{time.inspect}" end end end
def to_i
Time[http://ruby-doc.org/stdlib/libdoc/time/rdoc/Time.html] should be used for precision
Date[http://ruby-doc.org/stdlib/libdoc/date/rdoc/Date.html] and
In such cases, Ruby's core
1.year.to_i # => 31557600
# equivalent to 365.25.days.to_i
1.month.to_i # => 2592000
# equivalent to 30.days.to_i
and years are 365.25 days:
duration of some periods, e.g. months are always 30 days
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
def to_s @value.to_s end