module Timer

Time Utility

Public Class Methods

elapsed() click to toggle source

Gets elapsed time

# 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
start() click to toggle source

Starts the timer

# File lib/program.rb, line 14
def self.start
  @@timeStart = Process.clock_gettime( Process::CLOCK_MONOTONIC )
end
stop() click to toggle source

Stops the timer

# File lib/program.rb, line 21
def self.stop
  @@timeEnd = Process.clock_gettime( Process::CLOCK_MONOTONIC )
end