Class: ClickHouse::Type::DateTime64Type

Inherits:
BaseType
  • Object
show all
Defined in:
lib/click_house/type/date_time64_type.rb

Constant Summary collapse

BASE_FORMAT =
'%Y-%m-%d %H:%M:%S'
CAST_FORMAT =
"#{BASE_FORMAT}.%N"
SERIALIZE_FORMATS =
{
  0 => BASE_FORMAT,
  1 => "#{BASE_FORMAT}.%1N",
  2 => "#{BASE_FORMAT}.%2N",
  3 => "#{BASE_FORMAT}.%3N",
  4 => "#{BASE_FORMAT}.%4N",
  5 => "#{BASE_FORMAT}.%5N",
  6 => "#{BASE_FORMAT}.%6N",
  7 => "#{BASE_FORMAT}.%7N",
  8 => "#{BASE_FORMAT}.%8N",
  9 => "#{BASE_FORMAT}.%9N",
}.freeze

Instance Method Summary collapse

Methods inherited from BaseType

#cast_each, #container?, #ddl?, #map?, #serialize_each, #tuple?

Instance Method Details

#cast(value, precision = 0, tz = nil) ⇒ Object

Tick size (precision):

10-precision seconds.
Valid range: [ 0 : 9 ].
Typically are used - 3 (milliseconds), 6 (microseconds), 9 (nanoseconds).


25
26
27
28
29
30
31
32
33
# File 'lib/click_house/type/date_time64_type.rb', line 25

def cast(value, precision = 0, tz = nil)
  format = precision.zero? ? BASE_FORMAT : CAST_FORMAT

  if tz
    Time.find_zone(tz).strptime(value, format)
  else
    Time.strptime(value, format)
  end
end

#serialize(value, precision = 3, _tz = nil) ⇒ Object



35
36
37
# File 'lib/click_house/type/date_time64_type.rb', line 35

def serialize(value, precision = 3, _tz = nil)
  value.strftime(SERIALIZE_FORMATS.fetch(precision))
end