require 'date' module SearchLingo module Parsers module MDY US_DATE = %r{(?\d{1,2})/(?\d{1,2})(?:/(?\d{2}\d{2}?))?} 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] day = Integer(m[:d]) month = Integer(m[:m]) year = begin if month < relative_to.month || month == relative_to.month && day <= relative_to.day relative_to.year else relative_to.year - 1 end end Date.new year, month, day end rescue ArgumentError end module_function :parse end end end