Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add support for RepeaterDayName, RepeaterMonthName, Ordinal/ScalarDay and Time #153

Merged
merged 1 commit into from

2 participants

Kareem Kouddous Lee Jarvis
Kareem Kouddous

This allows for the following examples to be parsed correctly:

  • Thu Aug 10th
  • Thu Aug 10th at 4pm
  • Thu 10th
  • Thu 10th at 4pm
Lee Jarvis leejarvis merged commit 8042173 into from
Lee Jarvis
Collaborator

Awesome work, thanks a lot

Kareem Kouddous
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 14, 2012
  1. Added support for RepeaterDayName, RepeaterMonthName, Ordinal/ScalarD…

    Kareem Kouddous authored
    …ay and Time
This page is out of date. Refresh to see the latest.
50 lib/chronic/handlers.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
3  lib/chronic/parser.rb
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),
32 test/test_parsing.rb
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
Something went wrong with that request. Please try again.