Permalink
Browse files

Add handler for '5pm on May 27' and the like

  • Loading branch information...
1 parent bc84958 commit 2dd91432a5c39809c7d3bb3f8e928d86080b947e indirect committed Jan 29, 2008
Showing with 55 additions and 0 deletions.
  1. +18 −0 lib/chronic/handlers.rb
  2. +15 −0 lib/chronic/separator.rb
  3. +22 −0 test/test_parsing.rb
View
@@ -10,7 +10,9 @@ def definitions #:nodoc:
Handler.new([:repeater_month_name, :scalar_day, :scalar_year], :handle_rmn_sd_sy),
Handler.new([:repeater_month_name, :scalar_day, :scalar_year, :separator_at?, 'time?'], :handle_rmn_sd_sy),
Handler.new([:repeater_month_name, :scalar_day, :separator_at?, 'time?'], :handle_rmn_sd),
+ Handler.new([:repeater_time, :repeater_day_portion?, :separator_on?, :repeater_month_name, :scalar_day], :handle_rmn_sd_on),
Handler.new([:repeater_month_name, :ordinal_day, :separator_at?, 'time?'], :handle_rmn_od),
+ Handler.new([:repeater_time, :repeater_day_portion?, :separator_on?, :repeater_month_name, :ordinal_day], :handle_rmn_od_on),
Handler.new([:repeater_month_name, :scalar_year], :handle_rmn_sy),
Handler.new([:scalar_day, :repeater_month_name, :scalar_year, :separator_at?, 'time?'], :handle_sd_rmn_sy),
Handler.new([:scalar_month, :separator_slash_or_dash, :scalar_day, :separator_slash_or_dash, :scalar_year, :separator_at?, 'time?'], :handle_sm_sd_sy),
@@ -109,9 +111,25 @@ def handle_rmn_sd(tokens, options) #:nodoc:
handle_m_d(tokens[0].get_tag(RepeaterMonthName), tokens[1].get_tag(ScalarDay).type, tokens[2..tokens.size], options)
end
+ def handle_rmn_sd_on(tokens, options) #:nodoc:
+ if tokens.size > 3
+ handle_m_d(tokens[2].get_tag(RepeaterMonthName), tokens[3].get_tag(ScalarDay).type, tokens[0..1], options)
+ else
+ handle_m_d(tokens[1].get_tag(RepeaterMonthName), tokens[2].get_tag(ScalarDay).type, tokens[0..0], options)
+ end
+ end
+
def handle_rmn_od(tokens, options) #:nodoc:
handle_m_d(tokens[0].get_tag(RepeaterMonthName), tokens[1].get_tag(OrdinalDay).type, tokens[2..tokens.size], options)
end
+
+ def handle_rmn_od_on(tokens, options) #:nodoc:
+ if tokens.size > 3
+ handle_m_d(tokens[2].get_tag(RepeaterMonthName), tokens[3].get_tag(OrdinalDay).type, tokens[0..1], options)
+ else
+ handle_m_d(tokens[1].get_tag(RepeaterMonthName), tokens[2].get_tag(OrdinalDay).type, tokens[0..0], options)
+ end
+ end
def handle_rmn_sy(tokens, options) #:nodoc:
month = tokens[0].get_tag(RepeaterMonthName).index
View
@@ -7,6 +7,7 @@ def self.scan(tokens)
if t = self.scan_for_slash_or_dash(tokens[i]) then tokens[i].tag(t); next end
if t = self.scan_for_at(tokens[i]) then tokens[i].tag(t); next end
if t = self.scan_for_in(tokens[i]) then tokens[i].tag(t); next end
+ if t = self.scan_for_on(tokens[i]) then tokens[i].tag(t); next end
end
tokens
end
@@ -44,6 +45,14 @@ def self.scan_for_in(token)
return nil
end
+ def self.scan_for_on(token)
+ scanner = {/^on$/ => :on}
+ scanner.keys.each do |scanner_item|
+ return SeparatorOn.new(scanner[scanner_item]) if scanner_item =~ token.word
+ end
+ return nil
+ end
+
def to_s
'separator'
end
@@ -72,5 +81,11 @@ def to_s
super << '-in'
end
end
+
+ class SeparatorOn < Separator #:nodoc:
+ def to_s
+ super << '-on'
+ end
+ end
end
View
@@ -27,6 +27,17 @@ def test_parse_guess_dates
time = parse_now("may 28 at 5:32.19pm", :context => :past)
assert_equal Time.local(2006, 5, 28, 17, 32, 19), time
+ # rm_sd_on
+
+ time = parse_now("5pm on may 28")
+ assert_equal Time.local(2007, 5, 28, 17), time
+
+ time = parse_now("5pm may 28")
+ assert_equal Time.local(2007, 5, 28, 17), time
+
+ time = parse_now("5 on may 28", :ambiguous_time_range => :none)
+ assert_equal Time.local(2007, 5, 28, 05), time
+
# rm_od
time = parse_now("may 27th")
@@ -44,6 +55,17 @@ def test_parse_guess_dates
time = parse_now("may 27th at 5", :ambiguous_time_range => :none)
assert_equal Time.local(2007, 5, 27, 5), time
+ # rm_od_on
+
+ time = parse_now("5:00 pm may 27th", :context => :past)
+ assert_equal Time.local(2006, 5, 27, 17), time
+
+ time = parse_now("5pm on may 27th", :context => :past)
+ assert_equal Time.local(2006, 5, 27, 17), time
+
+ time = parse_now("5 on may 27th", :ambiguous_time_range => :none)
+ assert_equal Time.local(2007, 5, 27, 5), time
+
# rm_sy
time = parse_now("June 1979")

0 comments on commit 2dd9143

Please sign in to comment.