Skip to content

Commit ad4cd99

Browse files
committed
Test five significant figures of microseconds to reduce off-by-one-microsecond errors
1 parent f971f36 commit ad4cd99

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

spec/mysql2/result_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,30 @@
292292
expect(@test_result['date_time_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2010-04-04 11:44:00')
293293
end
294294

295+
it "should return Time values with microseconds" do
296+
now = Time.now
297+
if RUBY_VERSION =~ /1.8/ || @client.server_info[:id] / 100 < 506
298+
result = @client.query("SELECT CAST('#{now.strftime('%F %T %z')}' AS DATETIME) AS a")
299+
expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z'))
300+
else
301+
result = @client.query("SELECT CAST('#{now.strftime('%F %T.%6N %z')}' AS DATETIME(6)) AS a")
302+
# microseconds is 6 digits after the decimal, but only test on 5 significant figures
303+
expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z'))
304+
end
305+
end
306+
307+
it "should return DateTime values with microseconds" do
308+
now = DateTime.now
309+
if RUBY_VERSION =~ /1.8/ || @client.server_info[:id] / 100 < 506
310+
result = @client.query("SELECT CAST('#{now.strftime('%F %T %z')}' AS DATETIME) AS a")
311+
expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z'))
312+
else
313+
result = @client.query("SELECT CAST('#{now.strftime('%F %T.%6N %z')}' AS DATETIME(6)) AS a")
314+
# microseconds is 6 digits after the decimal, but only test on 5 significant figures
315+
expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z'))
316+
end
317+
end
318+
295319
if 1.size == 4 # 32bit
296320
klass = if RUBY_VERSION =~ /1.8/
297321
DateTime

spec/mysql2/statement_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ def stmt_count
150150
if RUBY_VERSION =~ /1.8/
151151
expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z'))
152152
else
153-
expect(result.first['a'].strftime('%F %T.%6N %z')).to eql(now.strftime('%F %T.%6N %z'))
153+
# microseconds is six digits after the decimal, but only test on 5 significant figures
154+
expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z'))
154155
end
155156
end
156157

@@ -161,7 +162,8 @@ def stmt_count
161162
if RUBY_VERSION =~ /1.8/
162163
expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z'))
163164
else
164-
expect(result.first['a'].strftime('%F %T.%6N %z')).to eql(now.strftime('%F %T.%6N %z'))
165+
# microseconds is six digits after the decimal, but only test on 5 significant figures
166+
expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z'))
165167
end
166168
end
167169

0 commit comments

Comments
 (0)