Module: RubyUnits::Time

Included in:
Time
Defined in:
lib/ruby_units/time.rb

Overview

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.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#+(other) ⇒ RubyUnits::Unit, ::Time

Parameters:

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
66
# 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) ⇒ RubyUnits::Unit, ::Time

Parameters:

Returns:



70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ RubyUnits::Unit

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.

Parameters:

Returns:



48
49
50
# 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