Class: ClickhouseRuby::Types::DateTime
- 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
-
#precision ⇒ Integer?
readonly
The precision for DateTime64 (nil for other types).
-
#timezone ⇒ String?
readonly
The timezone for DateTime types.
Attributes inherited from Base
Instance Method Summary collapse
-
#cast(value) ⇒ Time, ...
Converts a Ruby value to a Time or Date.
-
#date_only? ⇒ Boolean
Returns whether this is a date-only type (Date, Date32).
-
#deserialize(value) ⇒ Time, ...
Converts a value from ClickHouse to Ruby Time or Date.
-
#initialize(name, precision: nil, timezone: nil) ⇒ DateTime
constructor
A new instance of DateTime.
-
#serialize(value) ⇒ String
Converts a time/date to SQL literal.
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
#precision ⇒ Integer? (readonly)
The precision for DateTime64 (nil for other types)
23 24 25 |
# File 'lib/clickhouse_ruby/types/date_time.rb', line 23 def precision @precision end |
#timezone ⇒ String? (readonly)
The timezone for DateTime types
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
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)
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
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
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 |