Sha256: f4616e426cfef6f4d3f769eb534be735ecd77c7e58380a83d01eed1452fc723b

Contents?: true

Size: 1.58 KB

Versions: 31

Compression:

Stored size: 1.58 KB

Contents

require 'date'

class AdminDataDateValidation

  def self.validate_with_operator(input)
    return false if input.blank?

    input.strip!

    # replace multiple consecutive white spaces by one single whitespace
    input.gsub!(/\s+/,' ')
    operator, date = input.split
    return false if operator.nil?
    return false unless %w(> < >= <= =).include? operator
    validate(date)
  end

  # Usage:
  #
  # validate('13-feb-2008') # => time_object
  # validate('13-February-2008') # => time_object
  # validate('13-February-2008') # => time_object
  # validate('30-Feb-2008') #=> false
  #
  def self.validate(input)
    return false if input.nil?

    input.strip!

    # remove all the white space characters
    input.gsub!(/\s/,'')

    return false if input.length < 9

    dd,mm,yyyy = input.split('-')
    return false if dd.nil?
    return false if mm.nil?
    return false if yyyy.nil?

    # month must be of aleast three characters
    return false if mm.length < 3

    mm = mm.downcase

    #get only the first three characters
    mm = mm[0,3]

    months = {'jan' => 1,
      'feb' => 2,
      'mar' => 3,
      'apr' => 4,
      'may' => 5,
      'jun' => 6,
      'jul' => 7,
      'aug' => 8,
      'sep' => 9,
      'oct' => 10,
      'nov' => 11,
    'dec' => 12 }

    return false unless months.keys.include? mm

    mm = months[mm]
    mm = mm.to_i
    yyyy = yyyy.to_i
    dd = dd.to_i

    # validate date values
    begin
      Date.new(yyyy,mm,dd)
    rescue  => e
      return false
    end

    t = Time.now
    t.change(:year => yyyy, :month => mm, :day => dd, :hour => 0)
  end

end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
admin_data-1.0.10 lib/admin_data_date_validation.rb
admin_data-1.0.9 lib/admin_data_date_validation.rb
admin_data-1.0.8 lib/admin_data_date_validation.rb
admin_data-1.0.7 lib/admin_data_date_validation.rb
admin_data-1.0.6 lib/admin_data_date_validation.rb
admin_data-1.0.5 lib/admin_data_date_validation.rb
admin_data-1.0.4 lib/admin_data_date_validation.rb
admin_data-1.0.3 lib/admin_data_date_validation.rb
admin_data-1.0.2 lib/admin_data_date_validation.rb
admin_data-1.0.1 lib/admin_data_date_validation.rb
admin_data-1.0.0 lib/admin_data_date_validation.rb