Skip to content

Commit

Permalink
added recognition of dates that start with a day name and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
imme5150 committed Oct 19, 2011
1 parent fe6268e commit 1f02c77
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/chronic/chronic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ def definitions(options={})

:date => [
Handler.new([:repeater_day_name, :repeater_month_name, :scalar_day, :repeater_time, :separator_slash_or_dash?, :time_zone, :scalar_year], :handle_rdn_rmn_sd_t_tz_sy),
Handler.new([:repeater_day_name, :repeater_month_name, :scalar_day], :handle_rdn_rmn_sd),
Handler.new([:repeater_day_name, :repeater_month_name, :ordinal_day], :handle_rdn_rmn_od),
Handler.new([:scalar_year, :separator_slash_or_dash, :scalar_month, :separator_slash_or_dash, :scalar_day, :repeater_time, :time_zone], :handle_sy_sm_sd_t_tz),
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),
Expand Down
34 changes: 34 additions & 0 deletions lib/chronic/handlers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,40 @@ def handle_sm_sy(tokens, options)
end
end

# Handle RepeaterDayName RepeaterMonthName OrdinalDay
def handle_rdn_rmn_od(tokens, options)
month = tokens[1].get_tag(RepeaterMonthName)
day = tokens[2].get_tag(OrdinalDay).type
year = Chronic.now.year

return if month_overflow?(year, month.index, day)

begin
start_time = Chronic.time_class.local(year, month.index, day)
end_time = Chronic.time_class.local(year, month.index, day + 1)
Span.new(start_time, end_time)
rescue ArgumentError
nil
end
end

# Handle RepeaterDayName RepeaterMonthName ScalarDay
def handle_rdn_rmn_sd(tokens, options)
month = tokens[1].get_tag(RepeaterMonthName)
day = tokens[2].get_tag(ScalarDay).type
year = Chronic.now.year

return if month_overflow?(year, month.index, day)

begin
start_time = Chronic.time_class.local(year, month.index, day)
end_time = Chronic.time_class.local(year, month.index, day + 1)
Span.new(start_time, end_time)
rescue ArgumentError
nil
end
end

# anchors

# Handle repeaters
Expand Down
10 changes: 10 additions & 0 deletions test/test_parsing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,16 @@ def test_noon
assert_equal Time.local(2011, 1, 1, 12, 0), t1
end

def test_handle_rdn_rmn_sd
time = parse_now("Thu Aug 10")
assert_equal Time.local(2006, 8, 10, 12), time
end

def test_handle_rdn_rmn_od
time = parse_now("Thu Aug 10th")
assert_equal Time.local(2006, 8, 10, 12), time
end

private
def parse_now(string, options={})
Chronic.parse(string, {:now => TIME_2006_08_16_14_00_00 }.merge(options))
Expand Down

0 comments on commit 1f02c77

Please sign in to comment.