Class: Neo4j::Shared::TypeConverters::DateTimeConverter

Inherits:
BaseConverter
  • Object
show all
Defined in:
lib/neo4j/shared/type_converters.rb

Overview

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

Class Method Summary collapse

Methods inherited from BaseConverter

converted?, #supports_array?

Class Method Details

.convert_typeObject


164
165
166
# File 'lib/neo4j/shared/type_converters.rb', line 164

def convert_type
  DateTime
end

.db_typeObject


168
169
170
# File 'lib/neo4j/shared/type_converters.rb', line 168

def db_type
  Integer
end

.to_db(value) ⇒ Object

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


174
175
176
177
178
179
180
181
# File 'lib/neo4j/shared/type_converters.rb', line 174

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(value) ⇒ Object


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/neo4j/shared/type_converters.rb', line 183

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
        return value.to_datetime
      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