Sha256: 983aa74a9780db9169890aa011670c6ce957d6f1a43c255d350162d96e5ec901

Contents?: true

Size: 1.17 KB

Versions: 15

Compression:

Stored size: 1.17 KB

Contents

# -*- coding: binary -*-
module Rex

###
#
# Extended time related functions.
#
###
module ExtTime

  #
  # Convert seconds to a string that is broken down into years, days, hours,
  # minutes, and second.
  #
  def self.sec_to_s(seconds)
    parts = [ 31536000, 86400, 3600, 60, 1 ].map { |d|
      if ((c = seconds / d) > 0)
        seconds -= c.truncate * d
        c.truncate
      else
        0
      end
    }.reverse

    str = ''

    [ "sec", "min", "hour", "day", "year" ].each_with_index { |name, idx|
      next if (!parts[idx] or parts[idx] == 0)

      str = "#{parts[idx]} #{name + ((parts[idx] != 1) ? 's' :'')} " + str
    }

    str.empty? ? "0 secs" : str.strip
  end

  #
  # Converts a string in the form n years g days x hours y mins z secs.
  #
  def self.str_to_sec(str)
    fields = str.split(/ /)
    secs   = 0

    fields.each_with_index { |f, idx|
      val = 0
      case f
        when /^year/
          val = 31536000
        when /^day/
          val = 86400
        when /^hour/
          val = 3600
        when /^min/
          val = 60
        when /^sec/
          val = 1
      end

      secs += val * fields[idx-1].to_i
    }

    secs
  end

end

end

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
rex-2.0.13 lib/rex/time.rb
rex-2.0.12 lib/rex/time.rb
rex-2.0.11 lib/rex/time.rb
rex-2.0.10 lib/rex/time.rb
rex-2.0.9 lib/rex/time.rb
rex-2.0.8 lib/rex/time.rb
rex-2.0.7 lib/rex/time.rb
rex-2.0.5 lib/rex/time.rb
rex-2.0.4 lib/rex/time.rb
dstruct-0.0.1 lib/rex/time.rb
rex-2.0.3 lib/rex/time.rb
librex-0.0.999 lib/rex/time.rb
rex-2.0.2 lib/rex/time.rb
librex-0.0.71 lib/rex/time.rb
librex-0.0.70 lib/rex/time.rb