Skip to content

Commit c83a407

Browse files
Vats3dsVats-shivam
andauthored
[DPY-14] Keep server time zone in sync with client time zone (#186) (#188)
* [DPY-14] Keep server time zone in sync with client time zone (#186) * [DPY-14] Keep server time zone in sync with client time zone * removed extra spacing * added:check_session_version --------- Co-authored-by: Vats3ds <[email protected]> * updated test and timestamp from ticks function * removed:print and unnecessary imports --------- Co-authored-by: Vats Shivam <[email protected]>
1 parent afff314 commit c83a407

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

pynuodb/datatype.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,15 @@ def TimestampFromTicks(ticks, micro=0, zoneinfo=LOCALZONE):
183183
if y < 10000:
184184
dt = utc_TimeStamp(year=y, month=m, day=d, hour=hour,
185185
minute=min, second=sec, microsecond=micro)
186+
if zoneinfo is None:
187+
return dt.replace(tzinfo=None)
186188
dt = dt.astimezone(zoneinfo)
187189
else:
188190
# shift one day.
189191
dt = utc_TimeStamp(year=9999, month=12, day=31, hour=hour,
190192
minute=min, second=sec, microsecond=micro)
193+
if zoneinfo is None:
194+
return dt.replace(tzinfo=None)
191195
dt = dt.astimezone(zoneinfo)
192196
# add day back.
193197
dt += TimeDelta(days=1)

pynuodb/encodedsession.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,12 @@ def create_statement(self):
382382

383383
def __execute_postfix(self):
384384
# type: () -> None
385+
if self.__sessionVersion >= protocol.TIMESTAMP_WITHOUT_TZ:
386+
tzUpdate = self.getBoolean()
387+
if tzUpdate:
388+
server_tz = self.getValue()
389+
self.__timezone_name = server_tz
390+
385391
txid = self.getInt()
386392
sid = self.getInt()
387393
seqid = self.getInt()
@@ -1106,6 +1112,21 @@ def getScaledTimestamp(self):
11061112
return datatype.TimestampFromTicks(seconds, micros, self.timezone_info)
11071113

11081114
raise DataError('Not a scaled timestamp')
1115+
1116+
def getScaledTimestampNoTz(self):
1117+
# type: () -> datatype.Timestamp
1118+
"""Read the next Scaled Timestamp without timezone value off the session.
1119+
1120+
:rtype: datetime.datetime
1121+
"""
1122+
code = self._getTypeCode()
1123+
if code == protocol.SCALEDTIMESTAMPNOTZ:
1124+
scale = crypt.fromByteString(self._takeBytes(1))
1125+
stamp = crypt.fromSignedByteString(self._takeBytes(code - protocol.SCALEDTIMESTAMPNOTZLEN0))
1126+
seconds, micros = self.__unpack(scale, stamp)
1127+
return datatype.TimestampFromTicks(seconds, micros, None)
1128+
1129+
raise DataError('Not a scaled timestamp without time zone')
11091130

11101131
def getScaledDate(self):
11111132
# type: () -> datatype.Date
@@ -1266,6 +1287,9 @@ def getValue(self):
12661287
if code >= protocol.SCALEDDATELEN1 and code <= protocol.SCALEDDATELEN8:
12671288
return self.getScaledDate()
12681289

1290+
if code == protocol.SCALEDTIMESTAMPNOTZ:
1291+
return self.getScaledTimestampNoTz()
1292+
12691293
if code == protocol.NULL:
12701294
return self.getNull()
12711295

pynuodb/protocol.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
LOBSTREAM1 = 227
6262
LOBSTREAM4 = 230
6363
ARRAYLEN1 = 231
64+
SCALEDTIMESTAMPNOTZLEN0 = 233
6465
ARRAYLEN8 = 238
6566
SCALEDCOUNT3 = 239
6667
DEBUGBARRIER = 240
@@ -377,5 +378,5 @@ def lookup_code(error_code):
377378
# The newest feature this driver supports.
378379
# The server will negotiate the highest compatible version.
379380
CURRENT_PROTOCOL_MAJOR = 1
380-
CURRENT_PROTOCOL_VERSION = CURSOR_HOLDABILITY
381+
CURRENT_PROTOCOL_VERSION = TIMESTAMP_WITHOUT_TZ
381382
AUTH_TEST_STR = 'Success!'

tests/nuodb_types_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .mock_tzs import localize
1515

1616

17+
1718
class TestNuoDBTypes(nuodb_base.NuoBase):
1819
def test_boolean_types(self):
1920
con = self._connect()
@@ -109,12 +110,12 @@ def test_datetime_types(self):
109110

110111
cursor.execute("SELECT * FROM tmp")
111112
row = cursor.fetchone()
112-
113+
113114
assert len(row) == 4
114115
assert row[0] == datetime.date(2000, 1, 1)
115116
assert row[1] == datetime.time(5, 44, 33, 221100)
116117
assert row[2] == localize(datetime.datetime(2000, 1, 1, 5, 44, 33, 221100))
117-
assert row[3] == localize(datetime.datetime(2000, 1, 1, 5, 44, 33, 221100))
118+
assert localize(row[3]) == localize(datetime.datetime(2000, 1, 1, 5, 44, 33, 221100))
118119

119120

120121
def test_null_type(self):

0 commit comments

Comments
 (0)