Class: DuckMap::LastMod

Inherits:
Object
  • Object
show all
Defined in:
lib/duck_map/last_mod.rb

Overview

LastMod is a Date/Time parsing class.

Class Method Summary collapse

Class Method Details

.to_date(value) ⇒ DateTime

Parses a DateTime represented as a String into a DateTime value. If value is String, then, it is parsed and returns a DateTime Object if valid. Otherwise, it returns nil. If the value passed is a Date, Time, or DateTime, then, it is returned as is.

Parameters:

  • value (String)

    A DateTime represented as a String to parse or can be a Date, Time, or DateTime value.

Returns:

  • (DateTime)

    DateTime value.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/duck_map/last_mod.rb', line 13

def self.to_date(value)
  date_value = nil

  if value.kind_of?(String)

    formats = ["%m/%d/%Y %H:%M:%S", "%m-%d-%Y %H:%M:%S", "%Y-%m-%d %H:%M:%S", "%m/%d/%Y", "%m-%d-%Y", "%Y-%m-%d"]
    Time::DATE_FORMATS.each_value { |x| formats.push(x)}

    formats.each do |format|

      begin

        date_value = Time.strptime(value, format)
        break

      rescue Exception => e
        date_value = nil
      end
      
    end

  elsif value.kind_of?(Date) || value.kind_of?(Time) || value.kind_of?(DateTime)
    date_value = value
    
  end        

  return date_value
end