Class: ClickhouseRuby::Types::DateTime

Inherits:
Base
  • Object
show all
Defined in:
lib/clickhouse_ruby/types/date_time.rb

Overview

Type handler for ClickHouse date and datetime types

Handles: Date, Date32, DateTime, DateTime64

Date types:

  • Date: days since 1970-01-01 (range: 1970-01-01 to 2149-06-06)

  • Date32: days since 1970-01-01 (range: 1900-01-01 to 2299-12-31)

DateTime types:

  • DateTime: seconds since 1970-01-01 00:00:00 UTC

  • DateTime64(precision): with sub-second precision (0-9 decimal places)

Instance Attribute Summary collapse

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#==, #hash, #nullable?, #to_s

Constructor Details

#initialize(name, precision: nil, timezone: nil) ⇒ DateTime

Returns a new instance of DateTime.



29
30
31
32
33
# File 'lib/clickhouse_ruby/types/date_time.rb', line 29

def initialize(name, precision: nil, timezone: nil)
  super(name)
  @precision = precision
  @timezone = timezone
end

Instance Attribute Details

#precisionInteger? (readonly)

The precision for DateTime64 (nil for other types)

Returns:

  • (Integer, nil)

    precision in decimal places



23
24
25
# File 'lib/clickhouse_ruby/types/date_time.rb', line 23

def precision
  @precision
end

#timezoneString? (readonly)

The timezone for DateTime types

Returns:

  • (String, nil)

    timezone name



27
28
29
# File 'lib/clickhouse_ruby/types/date_time.rb', line 27

def timezone
  @timezone
end

Instance Method Details

#cast(value) ⇒ Time, ...

Converts a Ruby value to a Time or Date

Parameters:

  • value (Object)

    the value to convert

Returns:

  • (Time, Date, nil)

    the time/date value

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/clickhouse_ruby/types/date_time.rb', line 40

def cast(value)
  return nil if value.nil?

  case value
  when ::Time
    date_only? ? value.to_date : value
  when ::Date
    date_only? ? value : value.to_time
  when ::String
    parse_string(value)
  when ::Integer
    # Unix timestamp
    date_only? ? Time.at(value).to_date : Time.at(value)
  else
    raise_cast_error(value)
  end
end

#date_only?Boolean

Returns whether this is a date-only type (Date, Date32)

Returns:



94
95
96
# File 'lib/clickhouse_ruby/types/date_time.rb', line 94

def date_only?
  name.start_with?("Date") && !name.start_with?("DateTime")
end

#deserialize(value) ⇒ Time, ...

Converts a value from ClickHouse to Ruby Time or Date

Parameters:

  • value (Object)

    the value from ClickHouse

Returns:

  • (Time, Date, nil)

    the time/date value



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/clickhouse_ruby/types/date_time.rb', line 62

def deserialize(value)
  return nil if value.nil?

  case value
  when ::Time, ::Date
    date_only? ? value.to_date : value.to_time
  when ::String
    parse_string(value)
  when ::Integer
    date_only? ? Time.at(value).to_date : Time.at(value)
  else
    parse_string(value.to_s)
  end
end

#serialize(value) ⇒ String

Converts a time/date to SQL literal

Parameters:

  • value (Time, Date, nil)

    the value to serialize

Returns:

  • (String)

    the SQL literal



81
82
83
84
85
86
87
88
89
# File 'lib/clickhouse_ruby/types/date_time.rb', line 81

def serialize(value)
  return "NULL" if value.nil?

  if date_only?
    format_date(value)
  else
    format_datetime(value)
  end
end