DateTimeConverter

Converts DateTime objects to and from Java long types. Must be timezone UTC.

Constants

  • DATETIME_FORMAT

Methods

.convert_type

def convert_type
  DateTime
end

.converted?

def converted?(value)
  value.is_a?(db_type)
end

.db_type

def db_type
  Integer
end
.to_db

Converts the given DateTime (UTC) value to an Integer. DateTime values are automatically converted to UTC.

def to_db(value)
  value = value.new_offset(0) if value.respond_to?(:new_offset)

  args = [value.year, value.month, value.day]
  args += (value.class == Date ? [0, 0, 0] : [value.hour, value.min, value.sec])

  Time.utc(*args).to_i
end

.to_ruby

def to_ruby(value)
  return value if value.is_a?(DateTime)
  t = case value
      when Time
        return value.to_datetime.utc
      when Integer
        Time.at(value).utc
      when String
        DateTime.strptime(value, DATETIME_FORMAT)
      else
        fail ArgumentError, "Invalid value type for DateType property: #{value.inspect}"
      end

  DateTime.civil(t.year, t.month, t.day, t.hour, t.min, t.sec)
end