Class: Time

Inherits:
Object show all
Includes:
TimeOptions
Defined in:
lib/overload/time.rb,
lib/overload/blank.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TimeOptions

#current, #long, #short

Class Method Details

.ago(start_time, end_time = nil) ⇒ Object

How long ago?



65
66
67
68
# File 'lib/overload/time.rb', line 65

def ago start_time, end_time = nil
  start = Time.parse start_time.to_s if [String, Date].include?(start_time.class)
  TimeDifference.new(start || start_time, end_time, start_time.class).humanize
end

.agop(secs, desc = nil) ⇒ Object

Precise ago Time.agop(61) -> 1min 1sec Time.agop(1111) -> 18min 31sec



53
54
55
56
57
58
59
60
61
62
# File 'lib/overload/time.rb', line 53

def agop secs, desc = nil
  return '-' unless secs

  [[60, :sec], [60, :min], [24, :hrs], [356, :days], [1000, :years]].map do |count, name|
    if secs > 0
      secs, n = secs.divmod(count)
      "#{n.to_i}#{name}"
    end
  end.compact.reverse.slice(0,2).join(' ')
end

.for(value) ⇒ Object

Generates a Time object from the given value. Used by #expires and #last_modified. extracted from Sinatra



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/overload/time.rb', line 77

def for value
  if value.is_a? Numeric
    Time.at value
  elsif value.respond_to? :to_s
    Time.parse value.to_s
  else
    value.to_time
  end
rescue Exception
  raise ArgumentError, "unable to convert #{value.inspect} to a Time object"
end

.monotonicObject



70
71
72
# File 'lib/overload/time.rb', line 70

def monotonic
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

.speed(num = 1) ⇒ Object

prints proc speed of execution it will print first execution separate from the rest, used in cache testing



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/overload/time.rb', line 37

def speed num = 1
  start = Time.now
  yield
  total = Time.now - start
  puts 'Speed: %s sec for 1st run' % total.round(3)
  if num > 1
    start = Time.now
    (num - 1).times { yield }
    total = Time.now - start
    puts 'Other %s runs speed: %s sec, avg -> %s' % [num, total.round(3), (total/num).round(3)]
  end
end

Instance Method Details

#blank?Boolean

Returns:



60
61
62
# File 'lib/overload/blank.rb', line 60

def blank?
  false
end