Class: GMO::PG::Payload::TypecastableEpochTime

Inherits:
TypecastableInteger show all
Defined in:
lib/gmo-pg/http_resource/payload/typecast.rb

Constant Summary collapse

TIME_ZONE =
'+09:00'
FORMAT =
'%Y%m%d%H%M%S'

Instance Attribute Summary

Attributes inherited from TypecastableValue

#value

Instance Method Summary collapse

Methods inherited from TypecastableValue

#==, from_hash, #initialize

Constructor Details

This class inherits a constructor from GMO::PG::Payload::TypecastableValue

Instance Method Details

#formatObject



64
65
66
# File 'lib/gmo-pg/http_resource/payload/typecast.rb', line 64

def format
  FORMAT
end

#to_attributeObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gmo-pg/http_resource/payload/typecast.rb', line 68

def to_attribute
  time =
    case @value
    when Time
      @value
    when DateTime
      Time.new(@value.year, @value.month, @value.day, @value.hour, @value.min, @value.sec)
    else
      Time.strptime(@value, format) rescue return super
    end
  time.to_i rescue super
end

#to_payloadObject



81
82
83
# File 'lib/gmo-pg/http_resource/payload/typecast.rb', line 81

def to_payload
  Time.at(to_attribute).localtime(TIME_ZONE).strftime(format) rescue super
end