Class: Time

Inherits:
Object show all
Defined in:
lib/ruby_units/time.rb

Overview

calendar better.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.at(arg, ms = nil) ⇒ RubyUnits::Unit, Time

Convert a duration to a Time value by considering the duration to be the number of seconds since the epoch

Parameters:

  • arg (Time)
  • ms (Integer) (defaults to: nil)

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ruby_units/time.rb', line 18

def self.at(arg, ms=nil)
  case arg
  when Time
    unit_time_at(arg)
  when RubyUnits::Unit
    if ms
      unit_time_at(arg.convert_to("s").scalar, ms)
    else
      unit_time_at(arg.convert_to("s").scalar)
    end
  else
    ms.nil? ? unit_time_at(arg) : unit_time_at(arg, ms)
  end
end

.in(duration) ⇒ RubyUnits::Unit, Time

Examples:

Time.in '5 min'

Returns:



65
66
67
# File 'lib/ruby_units/time.rb', line 65

def self.in(duration)
  Time.now + duration.to_unit
end

.unit_time_atObject



10
# File 'lib/ruby_units/time.rb', line 10

alias unit_time_at at

Instance Method Details

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

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby_units/time.rb', line 48

def +(other)
  case other
  when RubyUnits::Unit
    other = other.convert_to('d').round.convert_to('s') if ['y', 'decade', 'century'].include? other.units  
    begin
      unit_add(other.convert_to('s').scalar)
    rescue RangeError
      self.to_datetime + other
    end
  else
    unit_add(other)
  end
end

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

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ruby_units/time.rb', line 72

def -(other)
  case other
  when RubyUnits::Unit
    other = other.convert_to('d').round.convert_to('s') if ['y', 'decade', 'century'].include? other.units  
    begin
      unit_sub(other.convert_to('s').scalar)
    rescue RangeError
      self.send(:to_datetime) - other
    end
  else
    unit_sub(other)
  end
end

#to_unit(other = nil) ⇒ Unit Also known as: unit, u

Returns:



34
35
36
# File 'lib/ruby_units/time.rb', line 34

def to_unit(other = nil)
  other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
end

#unit_addObject



46
# File 'lib/ruby_units/time.rb', line 46

alias :unit_add :+

#unit_subObject



69
# File 'lib/ruby_units/time.rb', line 69

alias :unit_sub :-