Class: Continuance::Duration

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/continuance/duration.rb

Overview

Duration is a measure of how long something takes to do where our resolution is limited to hours, minutes, seconds and nano seconds which should be enough to measure anything

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hours = 0, minutes = 0, seconds = 0, nano_seconds = 0) ⇒ Duration

Default constructor which creates a base time marker in order to calculate proper times when a duration is specified as a string



17
18
19
20
21
22
# File 'lib/continuance/duration.rb', line 17

def initialize(hours = 0, minutes = 0, seconds = 0, nano_seconds = 0)
  @hours = hours
  @minutes = minutes
  @seconds = seconds
  @nano_seconds = nano_seconds
end

Instance Attribute Details

#hoursObject (readonly)

Returns the value of attribute hours.



12
13
14
# File 'lib/continuance/duration.rb', line 12

def hours
  @hours
end

#minutesObject (readonly)

Returns the value of attribute minutes.



12
13
14
# File 'lib/continuance/duration.rb', line 12

def minutes
  @minutes
end

#nano_secondsObject (readonly)

Returns the value of attribute nano_seconds.



13
14
15
# File 'lib/continuance/duration.rb', line 13

def nano_seconds
  @nano_seconds
end

#secondsObject (readonly)

Returns the value of attribute seconds.



13
14
15
# File 'lib/continuance/duration.rb', line 13

def seconds
  @seconds
end

Class Method Details

.create(duration, format) ⇒ Object

A duration can be created by specifying the time as a string and providing a valid format for the time. The supported formats are listed at ruby-doc.org/core-2.2.1/Time.html



73
74
75
76
# File 'lib/continuance/duration.rb', line 73

def self.create(duration, format)
  time_val = Time.strptime(duration, format, BaseDate.new.val)
  Duration.new(time_val.hour, time_val.min, time_val.sec, time_val.nsec)
end

Instance Method Details

#+(other) ⇒ Object

Should be able to calculate the sum of two durations objects as seconds, this is returned as a float value



34
35
36
# File 'lib/continuance/duration.rb', line 34

def +(other)
  convert_to(to_f + other.to_f)
end

#-(other) ⇒ Object

Should be able to calculate the difference between two durations objects as seconds



26
27
28
29
30
# File 'lib/continuance/duration.rb', line 26

def -(other)
  other_time = Time.strptime(other.to_s, '%H:%M:%S.%N', BaseDate.new.val)
  self_time = Time.strptime(to_s, '%H:%M:%S.%N', BaseDate.new.val)
  convert_to(self_time - other_time)
end

#<=>(other) ⇒ Object

Comparable implementation for a duration object



60
61
62
63
64
65
66
67
68
# File 'lib/continuance/duration.rb', line 60

def <=>(other)
  if to_f < other.to_f
    -1
  elsif to_f > other.to_f
    1
  else
    0
  end
end

#eql?(other) ⇒ Boolean

Total equivalence implementation including the objects type



55
56
57
# File 'lib/continuance/duration.rb', line 55

def eql?(other)
  other.class == self.class && self == other
end

#to_fObject

Converts a duration object to it’s equivalent number of seconds, this value is returned as a float value



48
49
50
51
52
# File 'lib/continuance/duration.rb', line 48

def to_f
  # TODO: This calculation can result in floating point errors when the duration
  # is converted to and from a float value
  (@hours * 3600) + (@minutes * 60) + @seconds + (@nano_seconds.to_f / 10**9)
end

#to_sObject

A duration can be serialized to a string, this is used not only for visual purposes, but to also use in other computations like the difference operation



40
41
42
43
44
# File 'lib/continuance/duration.rb', line 40

def to_s
  sub_sec = @nano_seconds.to_f / (10**9)
  sub_sec_str = sub_sec.to_s.gsub('0.', '')
  "#{@hours}:#{@minutes}:#{@seconds}.#{sub_sec_str}"
end