Skip to content

Commit 378eb14

Browse files
committed
fix: Support time zone when serializing DateTimes
Previously a DateTime being serialized would ignore the timezone, effectively assuming the timezone of the value was the same as the column. Instead, the timezone is now taken into account so the time can be given in any timezone and it will be converted to the timezone of the column.
1 parent 663b72d commit 378eb14

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

lib/click_house/type/date_time64_type.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ def cast(value, precision = 0, tz = nil)
3232
end
3333
end
3434

35-
def serialize(value, precision = 3, _tz = nil)
36-
value.strftime(SERIALIZE_FORMATS.fetch(precision))
35+
def serialize(value, precision = 3, tz = nil)
36+
if tz
37+
value.in_time_zone(Time.find_zone(tz)).strftime(SERIALIZE_FORMATS.fetch(precision))
38+
else
39+
value.strftime(SERIALIZE_FORMATS.fetch(precision))
40+
end
3741
end
3842
end
3943
end

lib/click_house/type/date_time_type.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ def cast(value, tz = nil)
1313
end
1414
end
1515

16-
def serialize(value, *)
17-
value.strftime(FORMAT)
16+
def serialize(value, tz = nil)
17+
if tz
18+
value.in_time_zone(Time.find_zone(tz)).strftime(FORMAT)
19+
else
20+
value.strftime(FORMAT)
21+
end
1822
end
1923
end
2024
end

spec/click_house/type/date_time64_spec.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
RSpec.describe ClickHouse::Type::DateTime64Type do
2-
let(:precisions) do
2+
let(:precisions) do
33
(0..9).to_a
44
end
55

@@ -9,11 +9,24 @@
99
end
1010

1111
it 'works' do
12-
precisions.each do |precision|
12+
precisions.each do |precision|
1313
tail = "." + "0" * precision if precision > 0
1414
expect(subject.serialize(time, precision)).to eq("2019-01-01 09:05:06#{tail}")
1515
end
1616
end
17+
18+
context 'when zone exists' do
19+
let(:time) do
20+
Time.new(2019, 1, 1, 9, 5, 6, Time.find_zone('UTC'))
21+
end
22+
23+
it 'works' do
24+
precisions.each do |precision|
25+
tail = "." + "0" * precision if precision > 0
26+
expect(subject.serialize(time, precision, 'Europe/Kyiv')).to eq("2019-01-01 11:05:06#{tail}")
27+
end
28+
end
29+
end
1730
end
1831

1932
describe '#cast' do

spec/click_house/type/date_time_type_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
it 'works' do
88
expect(subject.serialize(time)).to eq('2019-01-01 09:05:06')
99
end
10+
11+
context 'when zone exists' do
12+
let(:time) do
13+
Time.new(2019, 1, 1, 9, 5, 6, Time.find_zone('UTC'))
14+
end
15+
16+
it 'works' do
17+
expect(subject.serialize(time, 'Europe/Kyiv')).to eq('2019-01-01 11:05:06')
18+
end
19+
end
1020
end
1121

1222
describe '#cast' do

0 commit comments

Comments
 (0)