Method: Doing::ChronifyString#chronify_qty

Defined in:
lib/doing/chronify/string.rb

#chronify_qtyInteger

Converts simple strings into seconds that can be added to a Time object

Input string can be HH:MM or XX[dhm][XXhm][XXm]

Returns:

  • (Integer)

    seconds



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/doing/chronify/string.rb', line 70

def chronify_qty
  minutes = 0
  case self.strip
  when /^(\d+):(\d\d)$/
    minutes += Regexp.last_match(1).to_i * 60
    minutes += Regexp.last_match(2).to_i
  when /^(\d+(?:\.\d+)?)([hmd])?/
    scan(/(\d+(?:\.\d+)?)([hmd])?/).each do |m|
      amt = m[0]
      type = m[1].nil? ? 'm' : m[1]

      minutes += case type.downcase
                 when 'm'
                   amt.to_i
                 when 'h'
                   (amt.to_f * 60).round
                 when 'd'
                   (amt.to_f * 60 * 24).round
                 else
                   0
                 end
    end
  end
  minutes * 60
end