Class: Time

Inherits:
Object show all
Defined in:
lib/epitools/core_ext/misc.rb,
lib/epitools/core_ext/numbers.rb

Instance Method Summary collapse

Instance Method Details

#in_wordsObject

Relative time, in words. (eg: “1 second ago”, “2 weeks from now”, etc.)



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/epitools/core_ext/numbers.rb', line 399

def in_words
  delta   = (Time.now-self).to_i
  a       = delta.abs
  
  amount  = case a
    when 0
      'just now'
    when 1
      '1 second'
    when 2..59
      "second".amount(a)
    when 1.minute...1.hour
      "minute".amount(a/1.minute)
    when 1.hour...1.day
      "hour".amount(a/1.hour)
    when 1.day...7.days
      "day".amount(a/1.day)
    when 1.week...1.month
      "week".amount(a/1.week)
    when 1.month...12.months
      "month".amount(a/1.month)
    else
      "year".amount(a/1.year)
  end
  
  if delta < 0
    amount += " from now"
  elsif delta > 0
    amount += " ago"
  end
  
  amount
end

#quarterObject

Which “quarter” of the year does this date fall into?



219
220
221
# File 'lib/epitools/core_ext/misc.rb', line 219

def quarter
  (month / 3.0).ceil
end