Skip to content

Commit e2ca27d

Browse files
authored
fix: escape timestamps for fields and tags (#131)
1 parent c13e5e1 commit e2ca27d

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
## 2.10.0 [unreleased]
1+
## 3.0.0 [unreleased]
2+
3+
### Bug Fixes
4+
1. [#131](https://github.com/influxdata/influxdb-client-ruby/pull/131): Convert time objects present in fields to integer. Prior to this change the timestamps were converted to strings
25

36
## 2.9.0 [2022-12-01]
47

lib/influxdb2/client/point.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def to_line_protocol
127127
return nil if fields.empty?
128128

129129
line_protocol << " #{fields}" if fields
130-
timestamp = _escape_time
130+
timestamp = _escape_time(@time)
131131
line_protocol << " #{timestamp}" if timestamp
132132

133133
line_protocol
@@ -185,23 +185,25 @@ def _escape_value(value)
185185
'"'.freeze + result + '"'.freeze
186186
elsif value.is_a?(Integer)
187187
"#{value}i"
188+
elsif value.is_a?(Time)
189+
"#{_escape_time(value)}i"
188190
elsif [Float::INFINITY, -Float::INFINITY].include?(value)
189191
''
190192
else
191193
value.to_s
192194
end
193195
end
194196

195-
def _escape_time
196-
if @time.nil?
197+
def _escape_time(value)
198+
if value.nil?
197199
nil
198-
elsif @time.is_a?(Integer)
199-
@time.to_s
200-
elsif @time.is_a?(Float)
201-
@time.round.to_s
202-
elsif @time.is_a?(Time)
203-
nano_seconds = @time.to_i * 1e9
204-
nano_seconds += @time.tv_nsec
200+
elsif value.is_a?(Integer)
201+
value.to_s
202+
elsif value.is_a?(Float)
203+
value.round.to_s
204+
elsif value.is_a?(Time)
205+
nano_seconds = value.to_i * 1e9
206+
nano_seconds += value.tv_nsec
205207
case @precision || DEFAULT_WRITE_PRECISION
206208
when InfluxDB2::WritePrecision::MILLISECOND then
207209
(nano_seconds / 1e6).round
@@ -213,7 +215,7 @@ def _escape_time
213215
nano_seconds.round
214216
end
215217
else
216-
@time.to_s
218+
value.to_s
217219
end
218220
end
219221
end

lib/influxdb2/client/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
# THE SOFTWARE.
2020

2121
module InfluxDB2
22-
VERSION = '2.10.0'.freeze
22+
VERSION = '3.0.0'.freeze
2323
end

test/influxdb/point_test.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def test_override_tag_and_field
6464
end
6565

6666
def test_field_types
67+
time = Time.utc(2023, 11, 1)
6768
point = InfluxDB2::Point.new(name: 'h2o')
6869
.add_tag('tag_b', 'b')
6970
.add_tag('tag_a', 'a')
@@ -73,8 +74,10 @@ def test_field_types
7374
.add_field('n4', 5.5)
7475
.add_field('bool', true)
7576
.add_field('string', 'string value')
77+
.add_field('started', time)
7678

77-
expected = 'h2o,tag_a=a,tag_b=b bool=true,n1=-2i,n2=10i,n3=1265437718438866624512i,n4=5.5,string="string value"'
79+
expected = 'h2o,tag_a=a,tag_b=b bool=true,n1=-2i,n2=10i,n3=1265437718438866624512i,n4=5.5,'\
80+
'started=1698796800000000000i,string="string value"'
7881
assert_equal expected, point.to_line_protocol
7982
end
8083

0 commit comments

Comments
 (0)