Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control how numbers on their own are treated #205

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Allow treating lonely numbers as dates
  • Loading branch information
andygeers committed Aug 22, 2013
commit aded0d072cae0001207ae3738e0d19f9ca2dccb0
10 changes: 10 additions & 0 deletions lib/chronic/handlers.rb
Original file line number Diff line number Diff line change
@@ -101,6 +101,16 @@ def handle_od(tokens, options)
raise e unless e.message =~ /out of range/
end

# Handle scalar-day
def handle_sd(tokens, options)
day = tokens[0].get_tag(ScalarDay)
t = Chronic.time_class.new(self.now.year, self.now.month, day.type)

Span.new(t, t + 1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here same...

rescue ArgumentError => e
raise e unless e.message =~ /out of range/
end

# Handle repeater-month-name/ordinal-day with separator-on
def handle_rmn_od_on(tokens, options)
if tokens.size > 3
8 changes: 7 additions & 1 deletion lib/chronic/parser.rb
Original file line number Diff line number Diff line change
@@ -12,7 +12,8 @@ class Parser
:guess => true,
:ambiguous_time_range => 6,
:endian_precedence => [:middle, :little],
:ambiguous_year_future_bias => 50
:ambiguous_year_future_bias => 50,
:ambiguous_number_priority => :time
}

attr_accessor :now
@@ -47,6 +48,9 @@ class Parser
# look x amount of years into the future and past. If the
# two digit year is `now + x years` it's assumed to be the
# future, `now - x years` is assumed to be the past.
# :ambiguous_number_priority - When parsing a number on its own (e.g. "1"),
# should it be treated as a time (1pm) or a date (1st of
# the current month)? Valid values are :time (default) or :date
def initialize(options = {})
@options = DEFAULT_OPTIONS.merge(options)
@now = options.delete(:now) || Chronic.time_class.now
@@ -204,6 +208,8 @@ def definitions(options = {})
]
}

@@definitions[:date] << Handler.new([:scalar_day], :handle_sd) if options[:ambiguous_number_priority] == :date

endians = [
Handler.new([:scalar_month, [:separator_slash, :separator_dash], :scalar_day, [:separator_slash, :separator_dash], :scalar_year, :separator_at?, 'time?'], :handle_sm_sd_sy),
Handler.new([:scalar_month, [:separator_slash, :separator_dash], :scalar_day, :separator_at?, 'time?'], :handle_sm_sd),
10 changes: 10 additions & 0 deletions test/test_parsing.rb
Original file line number Diff line number Diff line change
@@ -46,6 +46,16 @@ def test_handle_od
assert_equal Time.new(2013, 9, 28), time
end

def test_handle_sd
now = Time.new(2013, 8, 1)
time = Chronic.parse("28", :now => now, :ambiguous_number_priority => :date)
assert_equal Time.new(2013, 8, 28), time

now = Time.new(2013, 9, 1)
time = Chronic.parse("28", :now => now, :ambiguous_number_priority => :date)
assert_equal Time.new(2013, 9, 28), time
end

def test_handle_rmn_sd
time = parse_now("aug 3")
assert_equal Time.local(2006, 8, 3, 12), time