Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion _mssql.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ SQLVARBINARY = SYBVARBINARY
SQLVARCHAR = SYBVARCHAR
SQLUUID = 36

SQLDATE = 40
SQLTIME = 41
SQLDATETIME2 = 42

#######################
## Exception classes ##
#######################
Expand Down Expand Up @@ -795,13 +799,25 @@ cdef class MSSQLConnection:
ctx.prec = precision if precision > 0 else 1
return decimal.Decimal(_remove_locale(buf, converted_length).decode(self._charset))

elif dbtype == SQLDATETIM4:
elif dbtype in (SQLDATETIM4, SQLDATETIME2):
dbconvert(self.dbproc, dbtype, data, -1, SQLDATETIME,
<BYTE *>&dt, -1)
dbdatecrack(self.dbproc, &di, <DBDATETIME *><BYTE *>&dt)
return datetime.datetime(di.year, di.month, di.day,
di.hour, di.minute, di.second, di.millisecond * 1000)

elif dbtype == SQLDATE:
dbconvert(self.dbproc, dbtype, data, -1, SQLDATETIME,
<BYTE *>&dt, -1)
dbdatecrack(self.dbproc, &di, <DBDATETIME *><BYTE *>&dt)
return datetime.date(di.year, di.month, di.day)

elif dbtype == SQLTIME:
dbconvert(self.dbproc, dbtype, data, -1, SQLDATETIME,
<BYTE *>&dt, -1)
dbdatecrack(self.dbproc, &di, <DBDATETIME *><BYTE *>&dt)
return datetime.time(di.hour, di.minute, di.second, di.millisecond * 1000)

elif dbtype == SQLDATETIME:
dbdatecrack(self.dbproc, &di, <DBDATETIME *>data)
return datetime.datetime(di.year, di.month, di.day,
Expand Down
26 changes: 25 additions & 1 deletion tests/test_types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
import binascii
from datetime import time
from datetime import date
from datetime import datetime
import decimal
from decimal import Decimal as D
Expand Down Expand Up @@ -34,6 +36,9 @@ def typeeq(v1, v2):
float_no float,
money_no money,
stamp_datetime datetime,
stamp_date date,
stamp_time time,
stamp_datetime2 datetime2,
data_bit bit,
comment_vch varchar(50),
comment_nvch nvarchar(50),
Expand All @@ -44,7 +49,7 @@ def typeeq(v1, v2):
decimal_no decimal(38,2),
decimal_no2 decimal(38,10),
numeric_no numeric(38,8),
stamp_time timestamp,
stamp_timestamp timestamp,
uuid uniqueidentifier
)
"""
Expand Down Expand Up @@ -157,6 +162,25 @@ def test_datetime_params_as_dict(self):
typeeq(testval, colval)
eq_(testval, colval)

def test_date(self):
testval = date(2013, 1, 2)
colval = self.insert_and_select('stamp_date', testval, 's')
typeeq(testval, colval)
eq_(testval, colval)

def test_time(self):
testval = datetime(2013, 1, 2, 3, 4, 5, 3000)
colval = self.insert_and_select('stamp_time', testval, 's')
testval_no_date = testval.time()
typeeq(testval_no_date, colval)
eq_(testval_no_date, colval)

def test_datetime2(self):
testval = datetime(2013, 1, 2, 3, 4, 5, 3000)
colval = self.insert_and_select('stamp_datetime2', testval, 's')
typeeq(testval, colval)
eq_(testval, colval)

def test_decimal(self):
# test rounding down
origval = D('1.2345')
Expand Down