Class: Time

Inherits:
Object show all
Defined in:
lib/presenter/types.rb

Class Method Summary collapse

Class Method Details

.typecast(value, options = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/presenter/types.rb', line 46

def self.typecast(value, options = {})
  if value.is_a?(Time)
    value
  elsif value.is_a?(Hash)
    Time.local(*value.values_at(:year, :month, :day, :hour, :minute, :second))
  elsif value.is_a?(Date)
    Time.local(value.year, value.month, value.day)
  else
    begin
      dt = Date._strptime(value.to_s, options[:format] || "%a %b %d %H:%M:%S %Z %Y")
      time = Time.local(dt[:year], dt[:mon], dt[:mday], dt[:hour], dt[:min], dt[:sec])
      time += (time.utc_offset - dt[:offset]) if dt[:offset]
      time
    rescue StandardError => e
      nil
    end
  end
end