Class: TickTock::Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/tick-tock/duration.rb

Constant Summary collapse

MILLIS_IN_SECOND =
1000
MILLIS_IN_MINUTE =
MILLIS_IN_SECOND * 60
MILLIS_IN_HOUR =
MILLIS_IN_MINUTE * 60
MILLIS_IN_DAY =
MILLIS_IN_HOUR * 24
MILLIS_IN_WEEK =
MILLIS_IN_DAY * 7
MILLIS_IN_YEAR =
MILLIS_IN_WEEK * 52

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(millis = 0) ⇒ Duration

Returns a new instance of Duration.



28
29
30
31
# File 'lib/tick-tock/duration.rb', line 28

def initialize(millis = 0)
  @raw_millis = millis
  @formatter = TickTock::Formatter.new(self)
end

Instance Attribute Details

#raw_millisObject (readonly)

Returns the value of attribute raw_millis.



12
13
14
# File 'lib/tick-tock/duration.rb', line 12

def raw_millis
  @raw_millis
end

Class Method Details

.from_pieces(millis = 0, seconds = 0, minutes = 0, hours = 0, days = 0, weeks = 0, years = 0) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/tick-tock/duration.rb', line 18

def self.from_pieces(millis=0, seconds=0, minutes=0, hours=0, days=0, weeks=0, years=0)
  Duration.new(millis +
               seconds * MILLIS_IN_SECOND +
               minutes * MILLIS_IN_MINUTE +
               hours * MILLIS_IN_HOUR +
               days * MILLIS_IN_DAY +
               weeks * MILLIS_IN_WEEK +
               years * MILLIS_IN_YEAR)
end

.from_times(start, finish) ⇒ Object



14
15
16
# File 'lib/tick-tock/duration.rb', line 14

def self.from_times(start, finish)
  Duration.new(((finish - start) * 1000).to_i)
end

Instance Method Details

#+(other) ⇒ Object



33
34
35
# File 'lib/tick-tock/duration.rb', line 33

def +(other)
  Duration.new(raw_millis + other.raw_millis)
end

#-(other) ⇒ Object



37
38
39
# File 'lib/tick-tock/duration.rb', line 37

def -(other)
  Duration.new(raw_millis - other.raw_millis)
end

#daysObject



49
50
51
# File 'lib/tick-tock/duration.rb', line 49

def days
  (raw_millis - years_in_millis - weeks_in_millis) / MILLIS_IN_DAY
end

#hoursObject



53
54
55
# File 'lib/tick-tock/duration.rb', line 53

def hours
  (raw_millis - years_in_millis - weeks_in_millis - days_in_millis) / MILLIS_IN_HOUR
end

#inspectObject



73
74
75
# File 'lib/tick-tock/duration.rb', line 73

def inspect
  to_s
end

#millisObject



65
66
67
# File 'lib/tick-tock/duration.rb', line 65

def millis
  raw_millis - years_in_millis - weeks_in_millis - days_in_millis - hours_in_millis - minutes_in_millis - seconds_in_millis
end

#minutesObject



57
58
59
# File 'lib/tick-tock/duration.rb', line 57

def minutes
  (raw_millis - years_in_millis - weeks_in_millis - days_in_millis - hours_in_millis) / MILLIS_IN_MINUTE
end

#secondsObject



61
62
63
# File 'lib/tick-tock/duration.rb', line 61

def seconds
  (raw_millis - years_in_millis - weeks_in_millis - days_in_millis - hours_in_millis - minutes_in_millis) / MILLIS_IN_SECOND
end

#to_sObject



69
70
71
# File 'lib/tick-tock/duration.rb', line 69

def to_s
  @formatter.to_s
end

#weeksObject



45
46
47
# File 'lib/tick-tock/duration.rb', line 45

def weeks
  (raw_millis - years_in_millis) / MILLIS_IN_WEEK
end

#yearsObject



41
42
43
# File 'lib/tick-tock/duration.rb', line 41

def years
  raw_millis / MILLIS_IN_YEAR
end