2016-08-06 11:58:50 -04:00
|
|
|
require "active_support/core_ext/array/conversions"
|
|
|
|
require "active_support/core_ext/object/acts_like"
|
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
|
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
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
def initialize(value, parts) #:nodoc:
|
|
|
|
@value, @parts = value, parts
|
|
|
|
end
|
2007-12-14 21:27:41 -05:00
|
|
|
|
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
|
|
|
|
Duration.new(value + other.value, @parts + other.parts)
|
|
|
|
else
|
|
|
|
Duration.new(value + other, @parts + [[:seconds, other]])
|
|
|
|
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
|
|
|
|
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
|
|
|
|
# duration of some periods, e.g. months are always 30 days
|
|
|
|
# and years are 365.25 days:
|
|
|
|
#
|
|
|
|
# # equivalent to 30.days.to_i
|
|
|
|
# 1.month.to_i # => 2592000
|
|
|
|
#
|
|
|
|
# # equivalent to 365.25.days.to_i
|
|
|
|
# 1.year.to_i # => 31557600
|
|
|
|
#
|
|
|
|
# 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
|
|
|
def self.===(other) #:nodoc:
|
2010-01-07 03:05:06 -05:00
|
|
|
other.is_a?(Duration)
|
2010-01-07 19:43:25 -05:00
|
|
|
rescue ::NoMethodError
|
2010-01-07 03:05:06 -05:00
|
|
|
false
|
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
|
|
|
# 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
|
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
|
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
|
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
def respond_to_missing?(method, include_private = false) #:nodoc:
|
2014-08-20 02:56:54 -04:00
|
|
|
@value.respond_to?(method, include_private)
|
|
|
|
end
|
|
|
|
|
2014-09-14 09:16:51 -04:00
|
|
|
# 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 self.parse(iso8601duration)
|
|
|
|
parts = ISO8601Parser.new(iso8601duration).parse!
|
|
|
|
time = ::Time.current
|
|
|
|
new(time.advance(parts) - time, parts)
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2014-11-03 11:34:44 -05:00
|
|
|
delegate :<=>, to: :value
|
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
protected
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2008-04-20 22:57:04 -04:00
|
|
|
def sum(sign, time = ::Time.current) #:nodoc:
|
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
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
private
|
2007-12-14 21:27:41 -05:00
|
|
|
|
|
|
|
def method_missing(method, *args, &block) #:nodoc:
|
2010-08-30 15:50:44 -04:00
|
|
|
value.send(method, *args, &block)
|
2007-12-14 21:27:41 -05:00
|
|
|
end
|
2007-01-15 01:54:50 -05:00
|
|
|
end
|
2007-10-06 21:07:00 -04:00
|
|
|
end
|