Module: Irc::Utils::ParseTime
- Defined in:
- lib/rbot/core/utils/parse_time.rb
Constant Summary collapse
- FLOAT_RX =
/((?:\d*\.)?\d+)/
- ONE_TO_NINE =
{ :one => 1, :two => 2, :three => 3, :four => 4, :five => 5, :six => 6, :seven => 7, :eight => 8, :nine => 9, }
- ONE_TO_NINE_RX =
Regexp.new ONE_TO_NINE.keys.join('|')
- TEENS_ETC =
{ :an => 1, :a => 1, :ten => 10, :eleven => 11, :twelve => 12, :thirteen => 13, :fourteen => 14, :fifteen => 15, :sixteen => 16, :seventeen => 17, :eighteen => 18, :nineteen => 19, }
- TEENS_ETC_RX =
Regexp.new TEENS_ETC.keys.join('|')
- ENTIES =
{ :twenty => 20, :thirty => 30, :forty => 40, :fifty => 50, :sixty => 60, }
- ENTIES_RX =
Regexp.new ENTIES.keys.join('|')
- LITNUM_RX =
/(#{ONE_TO_NINE_RX})|(#{TEENS_ETC_RX})|(#{ENTIES_RX})\s*(#{ONE_TO_NINE_RX})?/
- FRACTIONS =
{ :"half" => 0.5, :"half a" => 0.5, :"half an" => 0.5, :"a half" => 0.5, :"a quarter" => 0.25, :"a quarter of" => 0.25, :"a quarter of a" => 0.25, :"a quarter of an" => 0.25, :"three quarter" => 0.75, :"three quarters" => 0.75, :"three quarter of" => 0.75, :"three quarters of" => 0.75, :"three quarter of a" => 0.75, :"three quarters of a" => 0.75, :"three quarter of an" => 0.75, :"three quarters of an" => 0.75, }
- FRACTION_RX =
Regexp.new FRACTIONS.keys.join('|')
- UNITSPEC_RX =
/(years?|months?|s(?:ec(?:ond)?s?)?|m(?:in(?:ute)?s?)?|h(?:(?:ou)?rs?)?|d(?:ays?)?|weeks?)/
Class Method Summary collapse
-
.parse_period(str) ⇒ Object
example: half an hour, two and a half weeks, 5 seconds, an hour and 5 minutes.
-
.time_unit(str) ⇒ Object
str must much UNITSPEC_RX.
Class Method Details
.parse_period(str) ⇒ Object
example: half an hour, two and a half weeks, 5 seconds, an hour and 5 minutes
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# 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 |
.time_unit(str) ⇒ Object
str must much UNITSPEC_RX
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# 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 |