Skip to content

Commit

Permalink
Add 'today' parameter to .parse method
Browse files Browse the repository at this point in the history
Refs mojombo#295
This one allows Chronic.parse method to receive
`today: true` param alongside a string to parse to allow
parsing ambiguous week days by name. E.g.:

Time.now  			     #=> Sun Aug 27 23:18:25 PDT 2006
Chronic.parse('sunday') 	     #=> Sun Sep 03 12:00:00 PDT 2006
Chronic.parse('sunday', today: true) #=> Sun Aug 27 12:00:00 PDT 2006
  • Loading branch information
novikserg committed May 15, 2015
1 parent 1b93e33 commit 5befd51
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ Chronic.parse('6/4/2012', :endian_precedence => :little)

Chronic.parse('INVALID DATE')
#=> nil

Chronic.parse('sunday')
#=> Sun Sep 03 12:00:00 PDT 2006

Chronic.parse('sunday', today: true)
#=> Sun Aug 27 12:00:00 PDT 2006
```

If the parser can find a date or time, either a Time or Chronic::Span
Expand Down
4 changes: 3 additions & 1 deletion lib/chronic/handlers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,9 @@ def get_anchor(tokens, options)
when :last
outer_span = head.next(:past)
when :this
if options[:context] != :past and repeaters.size > 0
if options[:today]
outer_span = head.this(:today)
elsif options[:context] != :past and repeaters.size > 0
outer_span = head.this(:none)
else
outer_span = head.this(options[:context])
Expand Down
8 changes: 7 additions & 1 deletion lib/chronic/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class Parser
:guess => true,
:ambiguous_time_range => 6,
:endian_precedence => [:middle, :little],
:ambiguous_year_future_bias => 50
:ambiguous_year_future_bias => 50,
:today => false
}

attr_accessor :now
Expand Down Expand Up @@ -54,6 +55,11 @@ 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.
# :today - When true is given, Chronic will parse day name as current
# date if they are ambigious. For example, Chronic.parse("monday")
# on 02/09/2015 will give "2015-02-16 12:00:00" without :today flag,
# and "2015-02-09 12:00:00" with `today: true`.

def initialize(options = {})
@options = DEFAULT_OPTIONS.merge(options)
@now = options.delete(:now) || Chronic.time_class.now
Expand Down
6 changes: 4 additions & 2 deletions lib/chronic/repeaters/repeater_day_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ def next(pointer)
super

direction = pointer == :future ? 1 : -1
direction = 0 if pointer == :today

day_num = symbol_to_number(@type)
raise ArgumentError, "It's not #{day_num.to_s} today" if pointer == :today && @now.wday != day_num

unless @current_date
@current_date = ::Date.new(@now.year, @now.month, @now.day)
@current_date += direction

day_num = symbol_to_number(@type)

while @current_date.wday != day_num
@current_date += direction
end
Expand Down
28 changes: 28 additions & 0 deletions test/test_parsing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,34 @@ def test_handle_rdn_rmn_od_sy
assert_equal Time.local(2005, 12, 30, 12), time
end

def test_get_anchor
time = parse_now("wednesday")
assert_equal Time.local(2006, 8, 23, 12), time

time = parse_now("this wednesday")
assert_equal Time.local(2006, 8, 23, 12), time

time = parse_now("wednesday at 16:30")
assert_equal Time.local(2006, 8, 23, 16, 30), time

time = parse_now("wednesday", today: true)
assert_equal Time.local(2006, 8, 16, 12), time

time = parse_now("this wednesday", today: true)
assert_equal Time.local(2006, 8, 16, 12), time

time = parse_now("wednesday at 16:00", today: true)
assert_equal Time.local(2006, 8, 16, 16), time

assert_raises(ArgumentError) do
parse_now("thursday at 16:00", today: true)
end

assert_raises(ArgumentError) do
parse_now("tuesday", today: true)
end
end

def test_normalizing_day_portions
assert_equal pre_normalize("8:00 pm February 11"), pre_normalize("8:00 p.m. February 11")
end
Expand Down

0 comments on commit 5befd51

Please sign in to comment.