Skip to content

Commit abe25b1

Browse files
committed
Fixing a small bug in the deprecated DBI::Timestamp conversion so it correctly converts nanosecond whole numbers to back to pre type cast SQL Server milliseconds, ultimately allow ruby's Time#usec which is microseconds to be correct.
1 parent c013d5b commit abe25b1

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

lib/core_ext/dbi.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
module SQLServerDBI
33

44
module Timestamp
5-
# Will further change DBI::Timestamp #to_s return value by limiting the usec of
6-
# the time to 3 digits and in some cases adding zeros if needed. For example:
7-
# "1985-04-15 00:00:00.0" # => "1985-04-15 00:00:00.000"
8-
# "2008-11-08 10:24:36.547000" # => "2008-11-08 10:24:36.547"
9-
# "2008-11-08 10:24:36.123" # => "2008-11-08 10:24:36.000"
5+
# Deprecated DBI. See documentation for Type::SqlserverTimestamp which
6+
# this method tries to mimic as ODBC is still going to convert SQL Server
7+
# milliconds to whole number representation of nanoseconds.
108
def to_sqlserver_string
11-
datetime, usec = to_s[0..22].split('.')
12-
"#{datetime}.#{sprintf("%03d",usec)}"
9+
datetime, nanoseconds = to_s.split('.')
10+
"#{datetime}.#{sprintf("%03d",nanoseconds.to_i/1000000)}"
1311
end
1412
end
1513

@@ -24,7 +22,7 @@ module Type
2422
# and the conversion we expect to have in the DB before type casting.
2523
#
2624
# "1985-04-15 00:00:00 0" # => "1985-04-15 00:00:00.000"
27-
# "2008-11-08 10:24:36 300000000" # => "2008-11-08 10:24:36.003"
25+
# "2008-11-08 10:24:36 30000000" # => "2008-11-08 10:24:36.003"
2826
# "2008-11-08 10:24:36 123000000" # => "2008-11-08 10:24:36.123"
2927
class SqlserverTimestamp
3028
def self.parse(obj)

0 commit comments

Comments
 (0)