Class: Time
- Includes:
- TimeOptions
- Defined in:
- lib/overload/time.rb,
lib/overload/blank.rb
Class Method Summary collapse
- .ago(start_time, end_time = nil) ⇒ Object
-
.for(value) ⇒ Object
Generates a Time object from the given value.
-
.humanize_seconds(secs) ⇒ Object
humanize_seconds(61) -> 1min 1sec humanize_seconds(1111) -> 18min 31sec.
- .monotonic ⇒ Object
Instance Method Summary collapse
Methods included from TimeOptions
Class Method Details
.ago(start_time, end_time = nil) ⇒ Object
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 |
# File 'lib/overload/time.rb', line 28 def ago start_time, end_time=nil start_time = Time.new(start_time.year, start_time.month, start_time.day) if start_time.class == Date end_time ||= Time.now time_diff = end_time.to_i - start_time.to_i in_past = time_diff > 0 ? true : false time_diff = time_diff.abs d_minutes = (time_diff / 60).round rescue 0 d_hours = (time_diff / (60 * 60)).round rescue 0 d_days = (time_diff / (60*60 * 24)).round rescue 0 d_months = (time_diff / (60*60*24 * 30)).round rescue 0 d_years = (time_diff / (60*60*24*30 * 12)).round rescue 0 return (in_past ? 'few sec ago' : 'in few seconds') if time_diff < 10 return (in_past ? 'less than min ago' : 'in less then a minute') if time_diff < 60 template = in_past ? '%s ago' : 'in %s' return template % d_minutes.pluralize('min') if d_minutes < 60 return template % d_hours.pluralize('hour') if d_hours < 24 return template % d_days.pluralize('day') if d_days < 31 return template % d_months.pluralize('month') if d_months < 12 return template % d_years.pluralize('year') end |
.for(value) ⇒ Object
Generates a Time object from the given value. Used by #expires and #last_modified. extracted from Sinatra
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/overload/time.rb', line 62 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 ArgumentError => boom raise boom rescue Exception raise ArgumentError, "unable to convert #{value.inspect} to a Time object" end |
.humanize_seconds(secs) ⇒ Object
humanize_seconds(61) -> 1min 1sec humanize_seconds(1111) -> 18min 31sec
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/overload/time.rb', line 17 def humanize_seconds secs return '-' unless secs secs = secs.to_i [[60, :sec], [60, :min], [24, :hrs], [356, :days], [1000, :years]].map{ |count, name| if secs > 0 secs, n = secs.divmod(count) "#{n.to_i}#{name}" end }.compact.reverse.slice(0,2).join(' ') end |
.monotonic ⇒ Object
55 56 57 |
# File 'lib/overload/time.rb', line 55 def monotonic Process.clock_gettime(Process::CLOCK_MONOTONIC) end |
Instance Method Details
#blank? ⇒ Boolean
55 56 57 |
# File 'lib/overload/blank.rb', line 55 def blank? false end |