Sha256: 23ad1b39c22a7c491a336c577c05abd50a5aaaa89726b44115aa9127cb91502a
Contents?: true
Size: 747 Bytes
Versions: 1
Compression:
Stored size: 747 Bytes
Contents
module Cogsworth class << self MULTIPLIERS = { "s" => (1), "m" => (60), "h" => (60 * 60), "d" => (60 * 60 * 24) } def string_to_seconds(string) string.gsub(' ','').downcase.scan(/(\d+)([a-zA-Z]+)/).inject(0) do |sum, pair| sum += pair[0].to_i * MULTIPLIERS[pair[1].slice(0,1)] end end def seconds_to_string(seconds, results=[]) return results.join(' ') if seconds == 0 ['d','h','m','s'].each do |unit| occurrences = seconds/MULTIPLIERS[unit] return seconds_to_string(seconds%MULTIPLIERS[unit], results.push("#{occurrences}#{unit}")) if occurrences > 0 end return seconds_to_string(seconds, results) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
cogsworth-1.0.2 | lib/cogsworth.rb |