class Zold::Age

Age

Public Class Methods

new(time, limit: nil) click to toggle source
# File lib/zold/age.rb, line 34
def initialize(time, limit: nil)
  @time = time.nil? || time.is_a?(Time) ? time : Txn.parse_time(time)
  @limit = limit
end

Public Instance Methods

to_s() click to toggle source
# File lib/zold/age.rb, line 39
def to_s
  return '?' if @time.nil?
  sec = Time.now - @time
  txt = text(sec)
  if !@limit.nil? && sec > @limit
    Rainbow(txt).red
  else
    txt
  end
end

Private Instance Methods

text(sec) click to toggle source
# File lib/zold/age.rb, line 52
def text(sec)
  return "#{(sec * 1_000_000).round}μs" if sec < 0.001
  return "#{(sec * 1000).round}ms" if sec < 1
  return "#{sec.round(2)}s" if sec < 60
  return "#{(sec / 60).round}m" if sec < 60 * 60
  hours = (sec / 3600).round
  return "#{hours}h" if hours < 24
  days = (hours / 24).round
  return "#{days}d" if days < 14
  return "#{(days / 7).round}w" if days < 40
  return "#{(days / 30).round}mo" if days < 365
  "#{(days / 365).round}y"
end