lib/search_lingo/parsers/mdy.rb in search_lingo-1.0.2 vs lib/search_lingo/parsers/mdy.rb in search_lingo-1.0.3
- old
+ new
@@ -11,12 +11,12 @@
##
# Returns a +Date+ object for the date represented by +term+. Returns
# +nil+ if +term+ can not be parsed.
#
- # If the year has two digits, it will be expanded into a four-digit by
- # +Date.parse+.
+ # If the year has two digits, it will be implicitly expanded into a
+ # four-digit year by +Date.parse+. Otherwise it will be used as is.
#
# If the year is omitted, it will be inferred using +relative_to+ as a
# reference date. In this scenario, the resulting date will always be
# less than or equal to the reference date. If +relative_to+ omitted, it
# defaults to today's date.
@@ -24,15 +24,16 @@
# Available as both a class method and an instance method.
def parse(term, relative_to: Date.today)
term.match /\A#{US_DATE}\z/ do |m|
return Date.parse "#{m[:y]}/#{m[:m]}/#{m[:d]}" if m[:y]
+ ref = relative_to
day = Integer(m[:d])
month = Integer(m[:m])
- year = if month < relative_to.month || month == relative_to.month && day <= relative_to.day
- relative_to.year
+ year = if month < ref.month || month == ref.month && day <= ref.day
+ ref.year
else
- relative_to.year - 1
+ ref.year - 1
end
Date.new year, month, day
end
rescue ArgumentError