FIX: Stored datetime.time values have the microseconds attribute set to zero#479
Open
FIX: Stored datetime.time values have the microseconds attribute set to zero#479
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes microsecond loss when round-tripping SQL Server TIME/TIME2 values by switching Python datetime.time binding to a text C-type with microsecond precision and updating the C++ fetch/batch-fetch paths to parse TIME2 from text back into datetime.time.
Changes:
- Bind Python
datetime.timeparameters as text (SQL_C_CHAR/SQL_C_WCHAR) usingisoformat(timespec="microseconds"). - Fetch
SQL_SS_TIME2as text in bothSQLGetData_wrapand batch fetch, converting todatetime.timevia a new parser. - Add a regression test asserting
TIME(6)preserves microseconds on insert/select.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| tests/test_004_cursor.py | Adds a regression test for TIME microsecond round-trip; minor formatting updates to SQL strings. |
| mssql_python/pybind/ddbc_bindings.cpp | Adds a TIME text parser and changes SQL_SS_TIME2 retrieval/binding to use SQL_C_CHAR buffers. |
| mssql_python/cursor.py | Changes TIME parameter mapping and normalizes TIME values to microsecond ISO text in execute/executemany binding. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
📊 Code Coverage Report
Diff CoverageDiff: main...HEAD, staged and unstaged changes
Summary
mssql_python/pybind/ddbc_bindings.cppLines 3331-3339 3331 } else {
3332 row.append(ParseSqlTimeTextToPythonObject(timeTextBuffer, timeDataLen));
3333 }
3334 } else {
! 3335 LOG("SQLGetData: Error retrieving SQL_SS_TIME2 for column "
3336 "%d - SQLRETURN=%d",
3337 i, ret);
3338 row.append(py::none());
3339 }📋 Files Needing Attention📉 Files with overall lowest coverage (click to expand)mssql_python.pybind.logger_bridge.hpp: 58.8%
mssql_python.pybind.logger_bridge.cpp: 59.2%
mssql_python.pybind.ddbc_bindings.h: 67.8%
mssql_python.pybind.ddbc_bindings.cpp: 70.4%
mssql_python.row.py: 70.5%
mssql_python.pybind.connection.connection.cpp: 75.3%
mssql_python.__init__.py: 77.1%
mssql_python.ddbc_bindings.py: 79.6%
mssql_python.pybind.connection.connection_pool.cpp: 79.6%
mssql_python.connection.py: 85.2%🔗 Quick Links
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Work Item / Issue Reference
Summary
This pull request introduces significant improvements to how SQL TIME/TIME2 values are handled in the MSSQL Python driver, transitioning from native C-type bindings to text-based representations. The changes ensure correct parsing, binding, and conversion between SQL TIME values and Python
datetime.timeobjects, addressing edge cases and improving compatibility.SQL TIME/TIME2 Handling Improvements
mssql_python/cursor.pyto useSQL_TYPE_TIMEand text C-types (SQL_C_CHAR), and normalized Pythondatetime.timevalues to ISO text format with microseconds.ddbc_bindings.cppto bind and fetch TIME/TIME2 columns as text buffers instead of native structs, and introduced a robust parser (ParseSqlTimeTextToPythonObject) for converting SQL time text to Python objects.SQL_TIME_TEXT_MAX_LEN).Testing and Utilities
Miscellaneous
drop_table_if_existsfor reliability.