Skip to content

Latest commit

 

History

History
120 lines (51 loc) · 1.69 KB

DateTimeConverter.rst

File metadata and controls

120 lines (51 loc) · 1.69 KB

DateTimeConverter

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

Constants

  • DATETIME_FORMAT

Files

Methods

.convert_type

ruby

def convert_type

DateTime

end

.db_type

ruby

def db_type

Integer

end

.to_db

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

ruby

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

ruby

def to_ruby(value)
t = case value
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