Skip to content

Commit

Permalink
Test five significant figures of microseconds to reduce off-by-one-mi…
Browse files Browse the repository at this point in the history
…crosecond errors
  • Loading branch information
sodabrew committed Nov 14, 2017
1 parent f971f36 commit 09fce2e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
24 changes: 24 additions & 0 deletions spec/mysql2/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,30 @@
expect(@test_result['date_time_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2010-04-04 11:44:00')
end

it "should return Time values with microseconds" do
now = Time.now
if RUBY_VERSION =~ /1.8/ || Gem::Version.new(@client.server_info.fetch(:version)) < Gem::Version.new('5.6')
result = @client.query("SELECT CAST('#{now.strftime('%F %T %z')}' AS DATETIME) AS a")
expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z'))
else
result = @client.query("SELECT CAST('#{now.strftime('%F %T.%6N %z')}' AS DATETIME(6)) AS a")
# microseconds is 6 digits after the decimal, but only test on 5 significant figures
expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z'))
end
end

it "should return DateTime values with microseconds" do
now = DateTime.now
if RUBY_VERSION =~ /1.8/ || Gem::Version.new(@client.server_info.fetch(:version)) < Gem::Version.new('5.6')
result = @client.query("SELECT CAST('#{now.strftime('%F %T %z')}' AS DATETIME) AS a")
expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z'))
else
result = @client.query("SELECT CAST('#{now.strftime('%F %T.%6N %z')}' AS DATETIME(6)) AS a")
# microseconds is 6 digits after the decimal, but only test on 5 significant figures
expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z'))
end
end

if 1.size == 4 # 32bit
klass = if RUBY_VERSION =~ /1.8/
DateTime
Expand Down
6 changes: 4 additions & 2 deletions spec/mysql2/statement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ def stmt_count
if RUBY_VERSION =~ /1.8/
expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z'))
else
expect(result.first['a'].strftime('%F %T.%6N %z')).to eql(now.strftime('%F %T.%6N %z'))
# microseconds is six digits after the decimal, but only test on 5 significant figures
expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z'))
end
end

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

Expand Down

0 comments on commit 09fce2e

Please sign in to comment.