Module: Timer

Defined in:
lib/program.rb

Overview

Time Utility

Constant Summary collapse

@@timeStart =
nil
@@timeEnd =
nil

Class Method Summary collapse

Class Method Details

.elapsedObject

Gets elapsed time



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/program.rb', line 28

def self.elapsed
  unless @@timeEnd.nil? || @@timeStart.nil?

    elapsed = @@timeEnd.to_i - @@timeStart.to_i # distance between t1 and t2 in seconds

    resolution = if elapsed > 29030400 # seconds in a year
      [(elapsed/29030400), 'years']
    elsif elapsed > 2419200
      [(elapsed/2419200), 'months']
    elsif elapsed > 604800
      [(elapsed/604800), 'weeks']
    elsif elapsed > 86400
      [(elapsed/86400), 'days']
    elsif elapsed > 3600 # seconds in an hour
      [(elapsed/3600), 'hours']
    elsif elapsed > 60
      [(elapsed/60), 'minutes']
    else
      [elapsed, 'seconds']
    end

    if resolution[0] == 1
      return resolution.join(' ')[0...-1]
    else
      return resolution.join(' ')
    end

  else
    return nil
  end
end

.startObject

Starts the timer



14
15
16
# File 'lib/program.rb', line 14

def self.start
  @@timeStart = Process.clock_gettime( Process::CLOCK_MONOTONIC )
end

.stopObject

Stops the timer



21
22
23
# File 'lib/program.rb', line 21

def self.stop
  @@timeEnd = Process.clock_gettime( Process::CLOCK_MONOTONIC )
end