huerlisi/bookyt

View on GitHub
lib/parse_date.rb

Summary

Maintainability
A
25 mins
Test Coverage
class Date
  # Date helpers
  def self.parse_date(value)
    if value.is_a?(String)
      if value.match /.*-.*-.*/
        return value
      end
      day, month, year = value.split('.').map {|s| s.to_i}
      month ||= Date.today.month
      year ||= Date.today.year
      year = expand_year(year, 1900)

      return sprintf("%4d-%02d-%02d", year, month, day)
    else
      return value
    end
  end

  def self.date_only_year?(value)
    value.is_a?(String) and value.strip.match /^\d{2,4}$/
  end

  def self.expand_year(value, base = 1900)
    year = value.to_i
    return year < 100 ? year + base : year
  end
end