Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/click_house/type/date_time64_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ def cast(value, precision = 0, tz = nil)
end
end

def serialize(value, precision = 3, _tz = nil)
value.strftime(SERIALIZE_FORMATS.fetch(precision))
def serialize(value, precision = 3, tz = nil)
if tz
value.in_time_zone(Time.find_zone(tz)).strftime(SERIALIZE_FORMATS.fetch(precision))
else
value.strftime(SERIALIZE_FORMATS.fetch(precision))
end
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/click_house/type/date_time_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ def cast(value, tz = nil)
end
end

def serialize(value, *)
value.strftime(FORMAT)
def serialize(value, tz = nil)
if tz
value.in_time_zone(Time.find_zone(tz)).strftime(FORMAT)
else
value.strftime(FORMAT)
end
end
end
end
Expand Down
17 changes: 15 additions & 2 deletions spec/click_house/type/date_time64_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RSpec.describe ClickHouse::Type::DateTime64Type do
let(:precisions) do
let(:precisions) do
(0..9).to_a
end

Expand All @@ -9,11 +9,24 @@
end

it 'works' do
precisions.each do |precision|
precisions.each do |precision|
tail = "." + "0" * precision if precision > 0
expect(subject.serialize(time, precision)).to eq("2019-01-01 09:05:06#{tail}")
end
end

context 'when zone exists' do
let(:time) do
Time.new(2019, 1, 1, 9, 5, 6, Time.find_zone('UTC'))
end

it 'works' do
precisions.each do |precision|
tail = "." + "0" * precision if precision > 0
expect(subject.serialize(time, precision, 'Europe/Kyiv')).to eq("2019-01-01 11:05:06#{tail}")
end
end
end
end

describe '#cast' do
Expand Down
10 changes: 10 additions & 0 deletions spec/click_house/type/date_time_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
it 'works' do
expect(subject.serialize(time)).to eq('2019-01-01 09:05:06')
end

context 'when zone exists' do
let(:time) do
Time.new(2019, 1, 1, 9, 5, 6, Time.find_zone('UTC'))
end

it 'works' do
expect(subject.serialize(time, 'Europe/Kyiv')).to eq('2019-01-01 11:05:06')
end
end
end

describe '#cast' do
Expand Down