module RubyUnits::Time

Time math is handled slightly differently. The difference is considered to be an exact duration if the subtracted value is in hours, minutes, or seconds. It is rounded to the nearest day if the offset is in years, decades, or centuries. This leads to less precise values, but ones that match the calendar better.

Public Instance Methods

+(other) click to toggle source

@param other [::Time, RubyUnits::Unit] @return [RubyUnits::Unit, ::Time]

Calls superclass method
# File lib/ruby_units/time.rb, line 54
def +(other)
  case other
  when RubyUnits::Unit
    other = other.convert_to("d").round.convert_to("s") if %w[y decade century].include? other.units
    begin
      super(other.convert_to("s").scalar)
    rescue RangeError
      to_datetime + other
    end
  else
    super
  end
end
-(other) click to toggle source

@param other [::Time, RubyUnits::Unit] @return [RubyUnits::Unit, ::Time]

Calls superclass method
# File lib/ruby_units/time.rb, line 70
def -(other)
  case other
  when RubyUnits::Unit
    other = other.convert_to("d").round.convert_to("s") if %w[y decade century].include? other.units
    begin
      super(other.convert_to("s").scalar)
    rescue RangeError
      public_send(:to_datetime) - other
    end
  else
    super
  end
end
to_unit(other = nil) click to toggle source

Convert a [::Time] object to a [RubyUnits::Unit] object. The time is considered to be a duration with the number of seconds since the epoch.

@param other [String, RubyUnits::Unit] @return [RubyUnits::Unit]

# File lib/ruby_units/time.rb, line 48
def to_unit(other = nil)
  other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
end