Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for parsing ordinal-dates/month-names/year

25th of May 1997
  • Loading branch information...
commit 0fc3f134d6dea982419985888f83e621ee6d7f36 1 parent 152124b
Lee Jarvis authored
View
2  HISTORY.md
@@ -1,5 +1,7 @@
# TBA
+* Added support for parsing ordinal-dates/month-names/year, ie:
+ `2nd of May 1995`
* Added support for parsing ordinal-dates and month names, ie:
`22nd of February at 6:30pm`
* Fix `Time.construct` leap year checking. Instead use `Date.leap?(year)`
View
1  README.md
@@ -103,6 +103,7 @@ Specific Dates
* January 5
* 22nd of june
+* 5th may 2017
* February twenty first
* dec 25
* may 27th
View
1  lib/chronic/chronic.rb
@@ -180,6 +180,7 @@ def definitions(options={})
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),
+ Handler.new([:ordinal_day, :repeater_month_name, :scalar_year, :separator_at?, 'time?'], :handle_od_rmn_sy),
Handler.new([:ordinal_day, :repeater_month_name, :separator_at?, 'time?'], :handle_od_rmn),
Handler.new([:repeater_time, :repeater_day_portion?, :separator_on?, :repeater_month_name, :ordinal_day], :handle_rmn_od_on),
Handler.new([:repeater_month_name, :scalar_year], :handle_rmn_sy),
View
16 lib/chronic/handlers.rb
@@ -103,6 +103,22 @@ def handle_rmn_od_sy(tokens, options) #:nodoc:
end
end
+ # Handle oridinal-day/repeater-month-name/scalar-year
+ def handle_od_rmn_sy(tokens, options) #:nodoc:
+ day = tokens[0].get_tag(OrdinalDay).type
+ month = tokens[1].get_tag(RepeaterMonthName).index
+ 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
+
# Handle scalar-day/repeater-month-name/scalar-year
def handle_sd_rmn_sy(tokens, options) #:nodoc:
new_tokens = [tokens[1], tokens[0], tokens[2]]
View
9 test/test_parsing.rb
@@ -256,6 +256,15 @@ def test_parse_od_rmn
assert_equal Time.local(2006, 12, 11, 8), time
end
+ # handle_od_rmn_sy
+ def test_parse_od_rmn_sy
+ time = parse_now("22nd February 2012")
+ assert_equal Time.local(2012, 2, 22, 12), time
+
+ time = parse_now("11th december 79")
+ assert_equal Time.local(1979, 12, 11, 12), time
+ end
+
def test_parse_two_digit_years
time = parse_now("may 97")
assert_equal Time.local(1997, 5, 16, 12), time
Please sign in to comment.
Something went wrong with that request. Please try again.