Sha256: fcded069d77f0346dbc30135e00b59c9298ddd0447f3d50fc24aa1ef376d0025

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

class Time
  def self.sentence seconds, specificity = 3

    return 'now' if seconds == 0

    ago = seconds < 0

    time = [
        { value: seconds.abs,   unit: 'second',      conversion: 60 },
        { value: 0,             unit: 'minute',      conversion: 60 },
        { value: 0,             unit: 'hour',        conversion: 24 },
        { value: 0,             unit: 'day',         conversion: 7 },
        { value: 0,             unit: 'week',        conversion: 4 },
        { value: 0,             unit: 'month',       conversion: 12 },
        { value: 0,             unit: 'year',        conversion: 10 },
        { value: 0,             unit: 'decade',      conversion: 100 },
        { value: 0,             unit: 'millennium',  conversion: nil },
    ]

    time.each_cons(2) do |this_time, next_time|
      next_time[:value], this_time[:value] = this_time[:value].divmod this_time[:conversion]
    end

    time = time.delete_if { |t| t[:value] == 0 }

    out = []
    time.reverse.each do |t|
      out << "#{t[:value]} #{t[:unit].pluralize t[:value]}"
      break if out.count == specificity
    end

    out.join(', ') + (ago ? ' ago' : '')

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
time_sentence-0.0.3 lib/ext/time.rb
time_sentence-0.0.2 lib/ext/time.rb