Skip to content

Commit 49a52e2

Browse files
committed
Add 'today' parameter to .parse method
Refs #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') Chronic.parse('sunday', today: true)
1 parent 1b93e33 commit 49a52e2

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ Chronic.parse('6/4/2012', :endian_precedence => :little)
4040

4141
Chronic.parse('INVALID DATE')
4242
#=> nil
43+
44+
Chronic.parse('sunday')
45+
#=> Sun Sep 03 12:00:00 PDT 2006
46+
47+
Chronic.parse('sunday', today: true)
48+
#=> Sun Aug 27 12:00:00 PDT 2006
4349
```
4450

4551
If the parser can find a date or time, either a Time or Chronic::Span

lib/chronic/handlers.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,9 @@ def get_anchor(tokens, options)
521521
when :last
522522
outer_span = head.next(:past)
523523
when :this
524-
if options[:context] != :past and repeaters.size > 0
524+
if options[:today]
525+
outer_span = head.this(:today)
526+
elsif options[:context] != :past and repeaters.size > 0
525527
outer_span = head.this(:none)
526528
else
527529
outer_span = head.this(options[:context])

lib/chronic/parser.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class Parser
1414
:guess => true,
1515
:ambiguous_time_range => 6,
1616
:endian_precedence => [:middle, :little],
17-
:ambiguous_year_future_bias => 50
17+
:ambiguous_year_future_bias => 50,
18+
:today => false
1819
}
1920

2021
attr_accessor :now
@@ -54,6 +55,11 @@ class Parser
5455
# look x amount of years into the future and past. If the
5556
# two digit year is `now + x years` it's assumed to be the
5657
# future, `now - x years` is assumed to be the past.
58+
# :today - When true is given, Chronic will parse day name as current
59+
# date if they are ambigious. For example, Chronic.parse("monday")
60+
# on 02/09/2015 will give "2015-02-16 12:00:00" without :today flag,
61+
# and "2015-02-09 12:00:00" with `today: true`.
62+
5763
def initialize(options = {})
5864
@options = DEFAULT_OPTIONS.merge(options)
5965
@now = options.delete(:now) || Chronic.time_class.now

lib/chronic/repeaters/repeater_day_name.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ def next(pointer)
1111
super
1212

1313
direction = pointer == :future ? 1 : -1
14+
direction = 0 if pointer == :today
15+
16+
day_num = symbol_to_number(@type)
17+
raise ArgumentError, "It's not #{day_num.to_s} today" if pointer == :today && @now.wday != day_num
1418

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

19-
day_num = symbol_to_number(@type)
20-
2123
while @current_date.wday != day_num
2224
@current_date += direction
2325
end

test/test_parsing.rb

+28
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,34 @@ def test_handle_rdn_rmn_od_sy
12391239
assert_equal Time.local(2005, 12, 30, 12), time
12401240
end
12411241

1242+
def test_get_anchor
1243+
time = parse_now("wednesday")
1244+
assert_equal Time.local(2006, 8, 23, 12), time
1245+
1246+
time = parse_now("this wednesday")
1247+
assert_equal Time.local(2006, 8, 23, 12), time
1248+
1249+
time = parse_now("wednesday at 16:30")
1250+
assert_equal Time.local(2006, 8, 23, 16, 30), time
1251+
1252+
time = parse_now("wednesday", today: true)
1253+
assert_equal Time.local(2006, 8, 16, 12), time
1254+
1255+
time = parse_now("this wednesday", today: true)
1256+
assert_equal Time.local(2006, 8, 16, 12), time
1257+
1258+
time = parse_now("wednesday at 16:00", today: true)
1259+
assert_equal Time.local(2006, 8, 16, 16), time
1260+
1261+
assert_raises(ArgumentError) do
1262+
parse_now("thursday at 16:00", today: true)
1263+
end
1264+
1265+
assert_raises(ArgumentError) do
1266+
parse_now("tuesday", today: true)
1267+
end
1268+
end
1269+
12421270
def test_normalizing_day_portions
12431271
assert_equal pre_normalize("8:00 pm February 11"), pre_normalize("8:00 p.m. February 11")
12441272
end

0 commit comments

Comments
 (0)