Method: Time#to_nice_distance_s

Defined in:
lib/texmailer/drafts.rb

#to_nice_distance_s(from = Time.now) ⇒ Object


291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/texmailer/drafts.rb', line 291

def to_nice_distance_s from=Time.now
  later_than = (self < from)
  diff = (self.to_i - from.to_i).abs.to_f
  text = 
    [ ["second", 60],
      ["minute", 60],
      ["hour", 24],
      ["day", 7],
      ["week", 4.345], # heh heh
      ["month", 12],
      ["year", nil],
    ].argfind do |unit, size|
    if diff.round <= 1
      "one #{unit}"
    elsif size.nil? || diff.round < size
      "#{diff.round} #{unit}s"
    else
      diff /= size.to_f
      false
    end
  end
  if later_than
    text + " ago"
  else
    "in " + text
  end  
end