module TimeHumanizer
Constants
- VALID_SECONDS
- VALID_TIME
- VERSION
Public Class Methods
to_humanized_time(seconds)
click to toggle source
Converts seconds to human readable format like 1h30m
# File lib/time_humanizer.rb, line 30 def self.to_humanized_time(seconds) unless blank_value?(seconds) raise InvalidArgument if VALID_SECONDS !~ seconds.to_s seconds = seconds.to_i hours = seconds / 3600 minutes = seconds % 3600 / 60 str = (hours > 0) ? "#{hours}h" : "" str << "#{minutes}m" if minutes > 0 str.size == 0 ? '0' : str end end
to_seconds(humanized_time_str)
click to toggle source
Converts human readable time format like 1h30m to seconds
# File lib/time_humanizer.rb, line 19 def self.to_seconds(humanized_time_str) unless blank_value?(humanized_time_str) match_data = humanized_time_str.match VALID_TIME raise InvalidArgument unless match_data h, m = match_data[:hours], match_data[:minutes] (h.to_i * 3600) + (m.to_i * 60) end end
Private Class Methods
blank_value?(value)
click to toggle source
# File lib/time_humanizer.rb, line 46 def self.blank_value?(value) value.nil? || (String === value && ( value.empty? || value.lstrip.empty? )) end