module Irc::Utils::ParseTime
Constants
- ENTIES
- ENTIES_RX
- FLOAT_RX
- FRACTIONS
- FRACTION_RX
- LITNUM_RX
- ONE_TO_NINE
- ONE_TO_NINE_RX
- TEENS_ETC
- TEENS_ETC_RX
- UNITSPEC_RX
Public Class Methods
Source
# File lib/rbot/core/utils/parse_time.rb, line 107 def ParseTime.parse_period(str) clean = str.gsub(/\s+/, ' ').strip sofar = 0 until clean.empty? if clean.sub!(/^(#{FRACTION_RX})\s+#{UNITSPEC_RX}/, '') # fraction followed by unit num = FRACTIONS[$1.intern] unit = ParseTime.time_unit($2) elsif clean.sub!(/^#{FLOAT_RX}\s*(?:\s+and\s+(#{FRACTION_RX})\s+)?#{UNITSPEC_RX}/, '') # float plus optional fraction followed by unit num = $1.to_f frac = $2 unit = ParseTime.time_unit($3) clean.strip! if frac.nil? and clean.sub!(/^and\s+(#{FRACTION_RX})/, '') frac = $1 end if frac num += FRACTIONS[frac.intern] end elsif clean.sub!(/^(?:#{LITNUM_RX})\s+(?:and\s+(#{FRACTION_RX})\s+)?#{UNITSPEC_RX}/, '') if $1 num = ONE_TO_NINE[$1.intern] elsif $2 num = TEENS_ETC[$2.intern] elsif $3 num = ENTIES[$3.intern] if $4 num += ONE_TO_NINE[$4.intern] end end frac = $5 unit = ParseTime.time_unit($6) clean.strip! if frac.nil? and clean.sub!(/^and\s+(#{FRACTION_RX})/, '') frac = $1 end if frac num += FRACTIONS[frac.intern] end else raise "invalid time string: #{clean} (parsed #{sofar} so far)" end sofar += num * unit clean.sub!(/^and\s+/, '') end return sofar end
example: half an hour, two and a half weeks, 5 seconds, an hour and 5 minutes
Source
# File lib/rbot/core/utils/parse_time.rb, line 83 def ParseTime.time_unit(str) case str[0,1].intern when :s 1 when :m if str[1,1] == 'o' # months 3600*24*30 else #minutes 60 end when :h 3600 when :d 3600*24 when :w 3600*24*7 when :y 3600*24*365 end end
str must much UNITSPEC_RX