lib/revs-utils.rb in revs-utils-1.0.11 vs lib/revs-utils.rb in revs-utils-1.0.12
- old
+ new
@@ -234,14 +234,17 @@
# tell us if the string passed is a valid year
def is_valid_year?(date_string,starting_year=1800)
date_string.to_s.strip.scan(/\D/).empty? and (starting_year..Date.today.year).include?(date_string.to_i)
end
- # tell us if the string passed is in is a full date of the format M/D/YYYY, and returns the date object if it is valid
+ # tell us if the string passed is in is a full date of the format M/D/YYYY or m-d-yyyy or m-d-yy or M/D/YY, and returns the date object if it is valid
def get_full_date(date_string)
begin
- date_obj=Date.strptime(date_string.gsub('-','/').delete(' '), '%m/%d/%Y')
- return (is_valid_year?(date_obj.year.to_s) ? date_obj : false)
+ date_obj_string=date_string.gsub('-','/').delete(' ')
+ date_format = (date_obj_string.split('/').last.size == 4 ? '%m/%d/%Y' : '%m/%d/%y') # if we think we have a 4 digit year, parse with 4 year format, else try the two year format
+ date_obj = Date.strptime(date_obj_string, date_format)
+ date_obj=date_obj.prev_year(100) if date_obj > Date.today # if the parsing yields a date in the future, this is a problem, so adjust back a century (due to this issue: http://stackoverflow.com/questions/27058068/ruby-incorrectly-parses-2-digit-year)
+ is_valid_year?(date_obj.year.to_s) ? date_obj : false
rescue
false
end
end