Permalink
Browse files

Merge pull request #153 from kareemk/master

Add support for RepeaterDayName, RepeaterMonthName, Ordinal/ScalarDay and Time
  • Loading branch information...
2 parents ed76d6e + cd78f11 commit 80421739a0da721dca831a43af3314b7d7f46294 @leejarvis leejarvis committed Nov 14, 2012
Showing with 79 additions and 6 deletions.
  1. +44 −6 lib/chronic/handlers.rb
  2. +3 −0 lib/chronic/parser.rb
  3. +32 −0 test/test_parsing.rb
View
@@ -274,14 +274,46 @@ def handle_sm_sy(tokens, options)
def handle_rdn_rmn_od(tokens, options)
month = tokens[1].get_tag(RepeaterMonthName)
day = tokens[2].get_tag(OrdinalDay).type
+ time_tokens = tokens.last(tokens.size - 3)
year = self.now.year
return if month_overflow?(year, month.index, day)
begin
- start_time = Chronic.time_class.local(year, month.index, day)
- end_time = time_with_rollover(year, month.index, day + 1)
- Span.new(start_time, end_time)
+ if time_tokens.empty?
+ start_time = Chronic.time_class.local(year, month.index, day)
+ end_time = time_with_rollover(year, month.index, day + 1)
+ Span.new(start_time, end_time)
+ else
+ day_start = Chronic.time_class.local(year, month.index, day)
+ day_or_time(day_start, time_tokens, options)
+ end
+ rescue ArgumentError
+ nil
+ end
+ end
+
+ # Handle RepeaterDayName OrdinalDay
+ def handle_rdn_od(tokens, options)
+ day = tokens[1].get_tag(OrdinalDay).type
+ time_tokens = tokens.last(tokens.size - 2)
+ year = self.now.year
+ month = self.now.month
+ if options[:context] == :future
+ self.now.day > day ? month += 1 : month
+ end
+
+ return if month_overflow?(year, month, day)
+
+ begin
+ if time_tokens.empty?
+ start_time = Chronic.time_class.local(year, month, day)
+ end_time = time_with_rollover(year, month, day + 1)
+ Span.new(start_time, end_time)
+ else
+ day_start = Chronic.time_class.local(year, month, day)
+ day_or_time(day_start, time_tokens, options)
+ end
rescue ArgumentError
nil
end
@@ -291,14 +323,20 @@ def handle_rdn_rmn_od(tokens, options)
def handle_rdn_rmn_sd(tokens, options)
month = tokens[1].get_tag(RepeaterMonthName)
day = tokens[2].get_tag(ScalarDay).type
+ time_tokens = tokens.last(tokens.size - 3)
year = self.now.year
return if month_overflow?(year, month.index, day)
begin
- start_time = Chronic.time_class.local(year, month.index, day)
- end_time = time_with_rollover(year, month.index, day + 1)
- Span.new(start_time, end_time)
+ if time_tokens.empty?
+ start_time = Chronic.time_class.local(year, month.index, day)
+ end_time = time_with_rollover(year, month.index, day + 1)
+ Span.new(start_time, end_time)
+ else
+ day_start = Chronic.time_class.local(year, month.index, day)
+ day_or_time(day_start, time_tokens, options)
+ end
rescue ArgumentError
nil
end
View
@@ -151,6 +151,9 @@ def definitions(options = {})
Handler.new([:repeater_day_name, :repeater_month_name, :scalar_day], :handle_rdn_rmn_sd),
Handler.new([:repeater_day_name, :repeater_month_name, :scalar_day, :scalar_year], :handle_rdn_rmn_sd_sy),
Handler.new([:repeater_day_name, :repeater_month_name, :ordinal_day], :handle_rdn_rmn_od),
+ Handler.new([:repeater_day_name, :repeater_month_name, :scalar_day, :separator_at?, 'time?'], :handle_rdn_rmn_sd),
+ Handler.new([:repeater_day_name, :repeater_month_name, :ordinal_day, :separator_at?, 'time?'], :handle_rdn_rmn_od),
+ Handler.new([:repeater_day_name, :ordinal_day, :separator_at?, 'time?'], :handle_rdn_od),
Handler.new([:scalar_year, :separator_slash_or_dash, :scalar_month, :separator_slash_or_dash, :scalar_day, :repeater_time, :time_zone], :handle_generic),
Handler.new([:repeater_month_name, :scalar_day, :scalar_year], :handle_rmn_sd_sy),
Handler.new([:repeater_month_name, :ordinal_day, :scalar_year], :handle_rmn_od_sy),
View
@@ -1055,6 +1055,38 @@ def test_handle_rdn_rmn_sd
assert_equal Time.local(2006, 12, 31, 12), time
end
+ def test_handle_rdn_rmn_sd_rt
+ time = parse_now("Thu Aug 10 4pm")
+ assert_equal Time.local(2006, 8, 10, 16), time
+
+ time = parse_now("Thu Aug 10 at 4pm")
+ assert_equal Time.local(2006, 8, 10, 16), time
+ end
+
+ def test_handle_rdn_rmn_od_rt
+ time = parse_now("Thu Aug 10th at 4pm")
+ assert_equal Time.local(2006, 8, 10, 16), time
+ end
+
+ def test_handle_rdn_od_rt
+ time = parse_now("Thu 17th at 4pm")
+ assert_equal Time.local(2006, 8, 17, 16), time
+
+ time = parse_now("Thu 16th at 4pm")
+ assert_equal Time.local(2006, 8, 16, 16), time
+
+ time = parse_now("Thu 1st at 4pm")
+ assert_equal Time.local(2006, 9, 1, 16), time
+
+ time = parse_now("Thu 1st at 4pm", :context => :past)
+ assert_equal Time.local(2006, 8, 1, 16), time
+ end
+
+ def test_handle_rdn_od
+ time = parse_now("Thu 17th")
+ assert_equal Time.local(2006, 8, 17, 12), time
+ end
+
def test_handle_rdn_rmn_sd_sy
time = parse_now("Thu Aug 10 2006")
assert_equal Time.local(2006, 8, 10, 12), time

0 comments on commit 8042173

Please sign in to comment.