class ExoBasic::Timer

Constants

END_OF_ALL_DAYS
EPOCH

Attributes

name[R]
now[R]
prev[R]
samples[R]
start[R]

Public Class Methods

add_days(t, days) click to toggle source
# File lib/exobasic/time/timer.rb, line 86
def self.add_days(t, days)
  t + days * 86400
end
add_sec(t, sec) click to toggle source
# File lib/exobasic/time/timer.rb, line 90
def self.add_sec(t, sec)
  t + sec
end
date_by_day(t) click to toggle source
YYYY, (1..366)
# File lib/exobasic/time/timer.rb, line 113
def self.date_by_day(t)
  [t.year, t.strftime('%-j').to_i]
end
date_by_day_to_epoch(tuple) click to toggle source
# File lib/exobasic/time/timer.rb, line 117
def self.date_by_day_to_epoch(tuple)
  Timer.to_epoch(Time.strptime("#{tuple[0]} #{tuple[1]}", '%Y %j'))
end
date_by_month(t) click to toggle source
YYYY, (1..12), (1..31)
# File lib/exobasic/time/timer.rb, line 144
def self.date_by_month(t)
  [t.year, t.month, t.strftime('%-d').to_i]
end
date_by_month_to_epoch(tuple) click to toggle source
# File lib/exobasic/time/timer.rb, line 148
def self.date_by_month_to_epoch(tuple)
  Timer.to_epoch(Time.strptime("#{tuple[0]} #{tuple[1]} #{tuple[2]}", '%Y %m %d'))
end
date_by_week(t) click to toggle source
YYYY, (1..54), (1..7)

week starts with Monday

# File lib/exobasic/time/timer.rb, line 127
def self.date_by_week(t)
  [t.year, t.strftime('%W').to_i + 1, t.strftime('%u').to_i]
end
date_by_week_to_epoch(tuple) click to toggle source
# File lib/exobasic/time/timer.rb, line 131
def self.date_by_week_to_epoch(tuple)
  Timer.to_epoch(Time.strptime("#{tuple[0]} #{tuple[1] - 1} #{tuple[2]}", '%Y %W %u'))
end
day_of_month(t) click to toggle source
# File lib/exobasic/time/timer.rb, line 156
def self.day_of_month(t)
  Timer.date_by_month(t)[2]
end
day_of_week_of_year(t) click to toggle source
# File lib/exobasic/time/timer.rb, line 139
def self.day_of_week_of_year(t)
  Timer.date_by_week(t)[2]
end
day_of_year(t) click to toggle source
# File lib/exobasic/time/timer.rb, line 121
def self.day_of_year(t)
  Timer.date_by_day(t)[1]
end
duration_in_days(from, to) click to toggle source
# File lib/exobasic/time/timer.rb, line 78
def self.duration_in_days(from, to)
  ((to - from) / 86400).to_i
end
duration_in_sec(from, to) click to toggle source
# File lib/exobasic/time/timer.rb, line 82
def self.duration_in_sec(from, to)
  to - from
end
from_epoch(epoch) click to toggle source

sec

# File lib/exobasic/time/timer.rb, line 95
def self.from_epoch(epoch)
  Time.at(epoch)
end
get(date_time) click to toggle source
# File lib/exobasic/time/timer.rb, line 68
def self.get(date_time)
  if date_time.nil?
    Timer.time_now
  elsif date_time.is_a?(String)
    Time.parse(date_time)
  else
    date_time
  end
end
month(t) click to toggle source
# File lib/exobasic/time/timer.rb, line 152
def self.month(t)
  Timer.date_by_month(t)[1]
end
new(name=nil) click to toggle source
# File lib/exobasic/time/timer.rb, line 8
def initialize(name=nil)
  @name    = name
  @start   = Time.now
  @prev    = @start
  @now     = @start
  @samples = 0
end
time_now() click to toggle source
# File lib/exobasic/time/timer.rb, line 64
def self.time_now
  Time.now
end
to_epoch(t) click to toggle source

sec

# File lib/exobasic/time/timer.rb, line 100
def self.to_epoch(t)
  t.strftime('%s').to_i
end
to_iso8601(t) click to toggle source
# File lib/exobasic/time/timer.rb, line 104
def self.to_iso8601(t)
  t.iso8601
end
tz_of(t) click to toggle source

(+, -)HH:MM

# File lib/exobasic/time/timer.rb, line 161
def self.tz_of(t)
  t.strftime('%:z')
end
week_of_year(t) click to toggle source
# File lib/exobasic/time/timer.rb, line 135
def self.week_of_year(t)
  Timer.date_by_week(t)[1]
end
year(t) click to toggle source
# File lib/exobasic/time/timer.rb, line 108
def self.year(t)
  t.year
end

Public Instance Methods

checkpoint(msg=nil) click to toggle source
# File lib/exobasic/time/timer.rb, line 59
def checkpoint(msg=nil)
  self.sample
  STDERR.puts self.show(msg)
end
delta() click to toggle source
# File lib/exobasic/time/timer.rb, line 32
def delta
  Time.now - @now
end
diff_from(that) click to toggle source
# File lib/exobasic/time/timer.rb, line 40
def diff_from(that)
  @now - that.now
end
lifetime() click to toggle source
# File lib/exobasic/time/timer.rb, line 36
def lifetime
  Time.now - @start
end
merge_with(that) click to toggle source
# File lib/exobasic/time/timer.rb, line 44
def merge_with(that)
  if that.start < @start
    @start    = that.start
    @samples += 1
  end
  if that.now > @now
    @now      = that.now
    @samples += 1
  end
end
sample() click to toggle source
# File lib/exobasic/time/timer.rb, line 16
def sample
  t         = Time.now
  @prev     = @now
  @now      = t
  @samples += 1
end
show(msg=nil) click to toggle source
# File lib/exobasic/time/timer.rb, line 55
def show(msg=nil)
  "-- TIME_MEASUREMENT #{@name}::#{msg}::#{@samples}\tfrom_prev = #{@now - @prev}\tfrom_start = #{@now - @start}"
end
tick() click to toggle source
# File lib/exobasic/time/timer.rb, line 23
def tick
  @now = Time.now
end
warp() click to toggle source
# File lib/exobasic/time/timer.rb, line 27
def warp
  @start   = @prev
  @samples = 1
end