Skip to content

Commit 199a9f7

Browse files
authored
Merge pull request #166 from ydb-platform/fix-date-in-params
can use datetime.date as param in execute
2 parents 282fe59 + 609b2d2 commit 199a9f7

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Fixed error while passing date parameter in execute
2+
13
## 2.12.2 ##
24
* Fix error of check retriable error for idempotent operations (error exist since 2.12.1)
35

tests/aio/test_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ async def test_types(driver, database, value, ydb_type):
6262
@pytest.mark.parametrize(
6363
"value,ydb_type,result_value",
6464
[
65-
# FIXME: TypeError: 'datetime.date'/'datetime.datetime' object cannot be interpreted as an integer
66-
# (test_today, 'Date', test_today),
65+
# FIXME: TypeError: 'datetime.datetime' object cannot be interpreted as an integer
6766
# (test_dt_today, "Datetime", test_dt_today),
67+
(test_today, "Date", test_today),
6868
(365, "Date", date(1971, 1, 1)),
6969
(3600 * 24 * 365, "Datetime", datetime(1971, 1, 1, 0, 0)),
7070
(test_td, "Interval", test_td),

ydb/types.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
_SECONDS_IN_DAY = 60 * 60 * 24
1414
_EPOCH = datetime(1970, 1, 1)
15+
1516
if six.PY3:
1617
_from_bytes = None
1718
else:
@@ -20,13 +21,20 @@ def _from_bytes(x, table_client_settings):
2021
return _utilities.from_bytes(x)
2122

2223

23-
def _from_date_number(x, table_client_settings):
24+
def _from_date(x, table_client_settings):
2425
if (
2526
table_client_settings is not None
2627
and table_client_settings._native_date_in_result_sets
2728
):
28-
return date.fromordinal(x + date(1970, 1, 1).toordinal())
29-
return x
29+
return _EPOCH.date() + timedelta(days=x.uint32_value)
30+
return x.uint32_value
31+
32+
33+
def _to_date(pb, value):
34+
if isinstance(value, date):
35+
pb.uint32_value = (value - _EPOCH.date()).days
36+
else:
37+
pb.uint32_value = value
3038

3139

3240
def _from_datetime_number(x, table_client_settings):
@@ -122,8 +130,9 @@ class PrimitiveType(enum.Enum):
122130
UUID = (_apis.primitive_types.UUID, None, _to_uuid, _from_uuid)
123131
Date = (
124132
_apis.primitive_types.DATE,
125-
"uint32_value",
126-
_from_date_number,
133+
None,
134+
_from_date,
135+
_to_date,
127136
)
128137
Datetime = (
129138
_apis.primitive_types.DATETIME,

0 commit comments

Comments
 (0)