4
4
import six
5
5
import json
6
6
from . import _utilities , _apis
7
- from datetime import date , datetime
7
+ from datetime import date , datetime , timedelta
8
8
import uuid
9
9
import struct
10
10
from google .protobuf import struct_pb2
11
11
12
12
13
+ _SECONDS_IN_DAY = 60 * 60 * 24
14
+ _EPOCH = datetime (1970 , 1 , 1 )
13
15
if six .PY3 :
14
16
_from_bytes = None
15
17
else :
@@ -56,6 +58,42 @@ def _from_uuid(pb, value):
56
58
pb .high_128 = struct .unpack ("Q" , value .bytes_le [8 :16 ])[0 ]
57
59
58
60
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
+
59
97
@enum .unique
60
98
class PrimitiveType (enum .Enum ):
61
99
"""
@@ -81,8 +119,7 @@ class PrimitiveType(enum.Enum):
81
119
Yson = _apis .primitive_types .YSON , "bytes_value"
82
120
Json = _apis .primitive_types .JSON , "text_value" , _from_json
83
121
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 )
86
123
Date = (
87
124
_apis .primitive_types .DATE ,
88
125
"uint32_value" ,
@@ -93,8 +130,18 @@ class PrimitiveType(enum.Enum):
93
130
"uint32_value" ,
94
131
_from_datetime_number ,
95
132
)
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
+ )
98
145
99
146
DyNumber = _apis .primitive_types .DYNUMBER , "text_value" , _from_bytes
100
147
0 commit comments