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.1.8 lib/admin_data_date_validation.rb
admin_data-1.1.7 lib/admin_data_date_validation.rb
admin_data-1.1.6 lib/admin_data_date_validation.rb
admin_data-1.1.5 lib/admin_data_date_validation.rb
admin_data-1.1.4 lib/admin_data_date_validation.rb
admin_data-1.1.3 lib/admin_data_date_validation.rb
admin_data-1.1.2 lib/admin_data_date_validation.rb
admin_data-1.1.1 lib/admin_data_date_validation.rb
admin_data-1.1.0 lib/admin_data_date_validation.rb
admin_data-1.0.22 lib/admin_data_date_validation.rb
admin_data-1.0.21 lib/admin_data_date_validation.rb
admin_data-1.0.19 lib/admin_data_date_validation.rb
admin_data-1.0.18 lib/admin_data_date_validation.rb
admin_data-1.0.17 lib/admin_data_date_validation.rb
admin_data-1.0.16 lib/admin_data_date_validation.rb
admin_data-1.0.15 lib/admin_data_date_validation.rb
admin_data-1.0.14 lib/admin_data_date_validation.rb
admin_data-1.0.13 lib/admin_data_date_validation.rb
admin_data-1.0.12 lib/admin_data_date_validation.rb
admin_data-1.0.11 lib/admin_data_date_validation.rb