44import six
55import json
66from . import _utilities , _apis
7- from datetime import date , datetime
7+ from datetime import date , datetime , timedelta
88import uuid
99import struct
1010from google .protobuf import struct_pb2
1111
1212
13+ _SECONDS_IN_DAY = 60 * 60 * 24
14+ _EPOCH = datetime (1970 , 1 , 1 )
1315if six .PY3 :
1416 _from_bytes = None
1517else :
@@ -56,6 +58,42 @@ def _from_uuid(pb, value):
5658 pb .high_128 = struct .unpack ("Q" , value .bytes_le [8 :16 ])[0 ]
5759
5860
61+ def _from_interval (value_pb , table_client_settings ):
62+ if (
63+ table_client_settings is not None
64+ and table_client_settings ._native_interval_in_result_sets
65+ ):
66+ return timedelta (microseconds = value_pb .int64_value )
67+ return value_pb .int64_value
68+
69+
70+ def _timedelta_to_microseconds (value ):
71+ return (value .days * _SECONDS_IN_DAY + value .seconds ) * 1000000 + value .microseconds
72+
73+
74+ def _to_interval (pb , value ):
75+ if isinstance (value , timedelta ):
76+ pb .int64_value = _timedelta_to_microseconds (value )
77+ else :
78+ pb .int64_value = value
79+
80+
81+ def _from_timestamp (value_pb , table_client_settings ):
82+ if (
83+ table_client_settings is not None
84+ and table_client_settings ._native_timestamp_in_result_sets
85+ ):
86+ return _EPOCH + timedelta (microseconds = value_pb .uint64_value )
87+ return value_pb .uint64_value
88+
89+
90+ def _to_timestamp (pb , value ):
91+ if isinstance (value , datetime ):
92+ pb .uint64_value = _timedelta_to_microseconds (value - _EPOCH )
93+ else :
94+ pb .uint64_value = value
95+
96+
5997@enum .unique
6098class PrimitiveType (enum .Enum ):
6199 """
@@ -81,8 +119,7 @@ class PrimitiveType(enum.Enum):
81119 Yson = _apis .primitive_types .YSON , "bytes_value"
82120 Json = _apis .primitive_types .JSON , "text_value" , _from_json
83121 JsonDocument = _apis .primitive_types .JSON_DOCUMENT , "text_value" , _from_json
84- UUID = _apis .primitive_types .UUID , None , _to_uuid , _from_uuid
85-
122+ UUID = (_apis .primitive_types .UUID , None , _to_uuid , _from_uuid )
86123 Date = (
87124 _apis .primitive_types .DATE ,
88125 "uint32_value" ,
@@ -93,8 +130,18 @@ class PrimitiveType(enum.Enum):
93130 "uint32_value" ,
94131 _from_datetime_number ,
95132 )
96- Timestamp = _apis .primitive_types .TIMESTAMP , "uint64_value"
97- Interval = _apis .primitive_types .INTERVAL , "int64_value"
133+ Timestamp = (
134+ _apis .primitive_types .TIMESTAMP ,
135+ None ,
136+ _from_timestamp ,
137+ _to_timestamp ,
138+ )
139+ Interval = (
140+ _apis .primitive_types .INTERVAL ,
141+ None ,
142+ _from_interval ,
143+ _to_interval ,
144+ )
98145
99146 DyNumber = _apis .primitive_types .DYNUMBER , "text_value" , _from_bytes
100147
0 commit comments