Class: DeltaT::TimeDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/delta_t/time_diff.rb

Constant Summary collapse

UNITS =
[:n_secs, :seconds, :minutes, :hours, :days, :months, :years]

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TimeDiff

Create a new TimeDiff either by two Time like objects or an hash with values for the time units (keys: seconds, minutes, hours, days, months, years)



7
8
9
10
11
12
13
14
15
16
# File 'lib/delta_t/time_diff.rb', line 7

def initialize *args
  if args.size == 1 && args[0].class == Hash
    @diff = [0,0,0,0,0,0,0]
    add_array [0, args[0][:seconds], args[0][:minutes], args[0][:hours], args[0][:days], args[0][:months], args[0][:years]]
  elsif args.size == 2 && args[0].respond_to?(:to_time) && args[1].respond_to?(:to_time)
    apply_time_diff args[0].to_time, args[1].to_time
  else
    raise ArgumentError, "Arguments neither two times nor a hash", caller
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



134
135
136
# File 'lib/delta_t/time_diff.rb', line 134

def method_missing m, *args, &block
  to_f.send m, *args, &block
end

Instance Method Details

#*(scalar) ⇒ Object

multiplies the duration of this TimeDiff with the given scalar. Must be an integer



88
89
90
91
92
93
94
95
96
# File 'lib/delta_t/time_diff.rb', line 88

def * scalar
  unless scalar.integer?
    raise ArgumentError, "Only integer calculations possible", caller
  end
  h = to_hash
  h.each { |k, v| h[k] = v * scalar }
  result = TimeDiff.new h
  result.normalize!
end

#+(other) ⇒ Object

Either adds two TimeDiffs together or advances a time by this TimeDiff



66
67
68
69
70
71
72
73
74
# File 'lib/delta_t/time_diff.rb', line 66

def + other
  if other.class == TimeDiff
    TimeDiff.new(other.to_hash).add_array @diff
  elsif other.respond_to? :to_time
    other.to_time.advance self.to_hash
  else
    raise ArgumentError, "Addition only defined for TimeDiff and time like classes", caller
  end
end

#-(other) ⇒ Object

Subtracts the other TimeDiff from this one



78
79
80
81
82
83
84
# File 'lib/delta_t/time_diff.rb', line 78

def - other
  if other.class == TimeDiff
    self + (-other)
  else
    raise ArgumentError, "Only subtraction of TimeDiffs possible", caller
  end
end

#-@Object

Equivalent to *-1



100
101
102
# File 'lib/delta_t/time_diff.rb', line 100

def -@
  self * -1
end

#<(other) ⇒ Object



48
49
50
# File 'lib/delta_t/time_diff.rb', line 48

def < other
  to_f < other.to_f
end

#<=(other) ⇒ Object



56
57
58
# File 'lib/delta_t/time_diff.rb', line 56

def <= other
  !(self > other)
end

#==(other) ⇒ Object

checks if two TimeDiffs are equal



40
41
42
43
44
45
46
# File 'lib/delta_t/time_diff.rb', line 40

def == other
  eql = true
  UNITS.each do |unit|
    eql &= send(unit) == other.send(unit)
  end
  eql
end

#>(other) ⇒ Object



52
53
54
# File 'lib/delta_t/time_diff.rb', line 52

def > other
  other < self
end

#>=(other) ⇒ Object



60
61
62
# File 'lib/delta_t/time_diff.rb', line 60

def >= other
  !(self < other)
end

#coerce(other) ⇒ Object



130
131
132
# File 'lib/delta_t/time_diff.rb', line 130

def coerce other
  return other, self.to_f
end

#to_fObject

Returns an float representing this TimeDiff - the total number of seconds with smaller units as decimals



112
113
114
# File 'lib/delta_t/time_diff.rb', line 112

def to_f
  to_i.to_f + @diff[0] * 0.000000001
end

#to_hashObject

Returns an hash with all different time durations with each ones value



118
119
120
121
122
123
124
# File 'lib/delta_t/time_diff.rb', line 118

def to_hash
  h = {}
  UNITS.each do |unit|
    h[unit] = send(unit)
  end
  h
end

#to_iObject

Returns an integer representing this TimeDiff - the total number of seconds



106
107
108
# File 'lib/delta_t/time_diff.rb', line 106

def to_i
  total_seconds
end

#to_sObject



126
127
128
# File 'lib/delta_t/time_diff.rb', line 126

def to_s
  to_f.to_s
end