Skip to content
Browse files

Add ordinal format support: ie 'February 14th, 2004'

  • Loading branch information...
1 parent faad1de commit 08dab52ad59c52b1c3846495b0c2f22a2b9ece1f @jfelchner jfelchner committed
Showing with 57 additions and 1 deletion.
  1. +2 −1 README.md
  2. +17 −0 lib/chronic/handlers.rb
  3. +38 −0 test/test_parsing.rb
View
3 README.md
@@ -105,6 +105,7 @@ Specific Dates
* oct 06
* jan 3 2010
* february 14, 2004
+* february 14th, 2004
* 3 jan 2000
* 17 april 85
* 5/27/1979
@@ -162,4 +163,4 @@ your changes merged back into core is as follows:
1. Do not change the version number, we will do that on our end
1. If necessary, rebase your commits into logical chunks, without errors
1. Push the branch up to GitHub
-1. Send a pull request for your branch
+1. Send a pull request for your branch
View
17 lib/chronic/handlers.rb
@@ -24,7 +24,9 @@ def definitions(options={}) #:nodoc:
: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_month_name, :scalar_day, :scalar_year], :handle_rmn_sd_sy),
+ Handler.new([:repeater_month_name, :ordinal_day, :scalar_year], :handle_rmn_od_sy),
Handler.new([:repeater_month_name, :scalar_day, :scalar_year, :separator_at?, 'time?'], :handle_rmn_sd_sy),
+ Handler.new([:repeater_month_name, :ordinal_day, :scalar_year, :separator_at?, 'time?'], :handle_rmn_od_sy),
Handler.new([:repeater_month_name, :scalar_day, :separator_at?, 'time?'], :handle_rmn_sd),
Handler.new([:repeater_time, :repeater_day_portion?, :separator_on?, :repeater_month_name, :scalar_day], :handle_rmn_sd_on),
Handler.new([:repeater_month_name, :ordinal_day, :separator_at?, 'time?'], :handle_rmn_od),
@@ -211,6 +213,21 @@ def handle_rmn_sd_sy(tokens, options) #:nodoc:
end
end
+ def handle_rmn_od_sy(tokens, options) #:nodoc:
+ month = tokens[0].get_tag(RepeaterMonthName).index
+ day = tokens[1].get_tag(OrdinalDay).type
+ year = tokens[2].get_tag(ScalarYear).type
+
+ time_tokens = tokens.last(tokens.size - 3)
+
+ begin
+ day_start = Chronic.time_class.local(year, month, day)
+ day_or_time(day_start, time_tokens, options)
+ rescue ArgumentError
+ nil
+ end
+ end
+
def handle_sd_rmn_sy(tokens, options) #:nodoc:
new_tokens = [tokens[1], tokens[0], tokens[2]]
time_tokens = tokens.last(tokens.size - 3)
View
38 test/test_parsing.rb
@@ -73,8 +73,46 @@ def test_parse_guess_dates
time = parse_now("dec 79")
assert_equal Time.local(1979, 12, 16, 12), time
+ # rm_od_sy
+
+ time = parse_now("November 18th 2010")
+ assert_equal Time.local(2010, 11, 18, 12), time
+
+ time = parse_now("November 18th, 2010")
+ assert_equal Time.local(2010, 11, 18, 12), time
+
+ time = parse_now("November 18th 2010 midnight")
+ assert_equal Time.local(2010, 11, 19, 0), time
+
+ time = parse_now("November 18th 2010 at midnight")
+ assert_equal Time.local(2010, 11, 19, 0), time
+
+ time = parse_now("November 18th 2010 at 4")
+ assert_equal Time.local(2010, 11, 18, 16), time
+
+ time = parse_now("November 18th 2010 at 4", :ambiguous_time_range => :none)
+ assert_equal Time.local(2010, 11, 18, 4), time
+
+ time = parse_now("March 30th, 1979")
+ assert_equal Time.local(1979, 3, 30, 12), time
+
+ time = parse_now("March 30th 79")
+ assert_equal Time.local(1979, 3, 30, 12), time
+
+ time = parse_now("March 30th 79 4:30")
+ assert_equal Time.local(1979, 3, 30, 16, 30), time
+
+ time = parse_now("March 30th 79 at 4:30", :ambiguous_time_range => :none)
+ assert_equal Time.local(1979, 3, 30, 4, 30), time
+
# rm_sd_sy
+ time = parse_now("November 18, 2010")
+ assert_equal Time.local(2010, 11, 18, 12), time
+
+ time = parse_now("February 14, 2004")
+ assert_equal Time.local(2004, 2, 14, 12), time
+
time = parse_now("jan 3 2010")
assert_equal Time.local(2010, 1, 3, 12), time

0 comments on commit 08dab52

Please sign in to comment.
Something went wrong with that request. Please try again.