Sha256: 9b1f5006274531a47dc56e1129977de6cb92a41604625255eab2202dae798f2f

Contents?: true

Size: 881 Bytes

Versions: 5

Compression:

Stored size: 881 Bytes

Contents

module Kubec
  module Utils
    # :nodoc:
    module HumanizeTime
      PAIRS = [
        [60, :seconds],
        [60, :minutes],
        [24, :hours],
        [1000, :days]
      ].freeze

      SHORT = {
        seconds: 's',
        minutes: 'm',
        hours:   'h',
        days:    'd'
      }.freeze

      class << self
        def humanize(secs, short: false, join: true)
          list = convert(secs).map do |(count, name)|
            if short
              "#{count}#{SHORT[name]}"
            else
              "#{count} #{name}"
            end
          end
          return list unless join
          list.join(' ')
        end

        def convert(secs)
          PAIRS.dup.map do |count, name|
            next unless secs > 0
            secs, n = secs.divmod(count)
            [n, name]
          end.compact.reverse
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
kubec-0.3.4 lib/kubec/utils/humanize_time.rb
kubec-0.3.3 lib/kubec/utils/humanize_time.rb
kubec-0.3.2 lib/kubec/utils/humanize_time.rb
kubec-0.3.1 lib/kubec/utils/humanize_time.rb
kubec-0.3.0 lib/kubec/utils/humanize_time.rb