Skip to content

Commit 00bab57

Browse files
committed
fix and test InfluxDB.now
1 parent abda7be commit 00bab57

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

lib/influxdb/timestamp_conversion.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def self.convert_timestamp(time, precision = "s")
3131
# InfluxDB.now("m") #=> 25726868
3232
# InfluxDB.now("h") #=> 428781
3333
def self.now(precision = "s")
34-
name, divisor = TIME_PRECISION_FACTORS.fetch(precision) do
34+
name, divisor = CLOCK_NAMES.fetch(precision) do
3535
raise ArgumentError, "invalid time precision: #{precision}"
3636
end
3737

@@ -56,8 +56,8 @@ def self.now(precision = "s")
5656
"u" => [:microsecond, 1],
5757
"ms" => [:millisecond, 1],
5858
"s" => [:second, 1],
59-
"m" => [:second, 1.to_r / 60],
60-
"h" => [:second, 1.to_r / 60 / 60],
59+
"m" => [:second, 60.to_r],
60+
"h" => [:second, (60 * 60).to_r],
6161
}.freeze
6262
private_constant :CLOCK_NAMES
6363
end

spec/influxdb/time_conversion_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,23 @@
2323
.to raise_exception(/invalid time precision.*whatever/i)
2424
end
2525
end
26+
27+
describe ".now" do
28+
{
29+
"ns" => [:nanosecond, 1_513_009_229_111_222_333],
30+
nil => [:nanosecond, 1_513_009_229_111_222_333],
31+
"u" => [:microsecond, 1_513_009_229_111_222],
32+
"ms" => [:millisecond, 1_513_009_229_111],
33+
"s" => [:second, 1_513_009_229],
34+
"m" => [:second, 25_216_820, 1_513_009_229],
35+
"h" => [:second, 420_280, 1_513_009_229],
36+
}.each do |precision, (name, expected, stub)|
37+
it "should return the current time in #{precision.inspect}" do
38+
expect(Process).to receive(:clock_gettime)
39+
.with(Process::CLOCK_REALTIME, name)
40+
.and_return(stub || expected)
41+
expect(described_class.now(precision)).to eq(expected)
42+
end
43+
end
44+
end
2645
end

0 commit comments

Comments
 (0)