Skip to content

Commit b347653

Browse files
authored
use timezone-aware API to avoid deprecated warning (#1213)
before this change, when testing with cqlsh, we have warnings like: ``` <frozen importlib._bootstrap>:488: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC). ```` in this change, we replace the deprecated API with timezone-aware API, to avoid this warning. to keep the backward compatibility, `DateTime.to_python()` still returns an offset-naive timestamp. Signed-off-by: Kefu Chai <[email protected]>
1 parent 7cdbdbb commit b347653

File tree

8 files changed

+22
-22
lines changed

8 files changed

+22
-22
lines changed

cassandra/cqlengine/columns.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
from copy import deepcopy, copy
16-
from datetime import date, datetime, timedelta
16+
from datetime import date, datetime, timedelta, timezone
1717
import logging
1818
from uuid import UUID as _UUID
1919

@@ -551,7 +551,7 @@ def to_python(self, value):
551551
elif isinstance(value, date):
552552
return datetime(*(value.timetuple()[:6]))
553553

554-
return datetime.utcfromtimestamp(value)
554+
return datetime.fromtimestamp(value, tz=timezone.utc).replace(tzinfo=None)
555555

556556
def to_database(self, value):
557557
value = super(DateTime, self).to_database(value)

cassandra/query.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"""
2020

2121
from collections import namedtuple
22-
from datetime import datetime, timedelta
22+
from datetime import datetime, timedelta, timezone
2323
import re
2424
import struct
2525
import time
@@ -1085,7 +1085,7 @@ class TraceEvent(object):
10851085

10861086
def __init__(self, description, timeuuid, source, source_elapsed, thread_name):
10871087
self.description = description
1088-
self.datetime = datetime.utcfromtimestamp(unix_time_from_uuid1(timeuuid))
1088+
self.datetime = datetime.fromtimestamp(unix_time_from_uuid1(timeuuid), tz=timezone.utc)
10891089
self.source = source
10901090
if source_elapsed is not None:
10911091
self.source_elapsed = timedelta(microseconds=source_elapsed)

cassandra/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838

3939
from cassandra import DriverException
4040

41-
DATETIME_EPOC = datetime.datetime(1970, 1, 1)
42-
UTC_DATETIME_EPOC = datetime.datetime.utcfromtimestamp(0)
41+
DATETIME_EPOC = datetime.datetime(1970, 1, 1).replace(tzinfo=None)
42+
UTC_DATETIME_EPOC = datetime.datetime.fromtimestamp(0, tz=datetime.timezone.utc).replace(tzinfo=None)
4343

4444
_nan = float('nan')
4545

tests/integration/cqlengine/columns/test_validation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import unittest
1616

1717
import sys
18-
from datetime import datetime, timedelta, date, tzinfo, time
18+
from datetime import datetime, timedelta, date, tzinfo, time, timezone
1919
from decimal import Decimal as D
2020
from uuid import uuid4, uuid1
2121
from packaging.version import Version
@@ -97,7 +97,7 @@ def test_datetime_timestamp(self):
9797
dt_value = 1454520554
9898
self.DatetimeTest.objects.create(test_id=5, created_at=dt_value)
9999
dt2 = self.DatetimeTest.objects(test_id=5).first()
100-
self.assertEqual(dt2.created_at, datetime.utcfromtimestamp(dt_value))
100+
self.assertEqual(dt2.created_at, datetime.fromtimestamp(dt_value, tz=timezone.utc).replace(tzinfo=None))
101101

102102
def test_datetime_large(self):
103103
dt_value = datetime(2038, 12, 31, 10, 10, 10, 123000)
@@ -809,7 +809,7 @@ def test_conversion_specific_date(self):
809809
assert isinstance(uuid, UUID)
810810

811811
ts = (uuid.time - 0x01b21dd213814000) / 1e7 # back to a timestamp
812-
new_dt = datetime.utcfromtimestamp(ts)
812+
new_dt = datetime.fromtimestamp(ts, tz=timezone.utc).replace(tzinfo=None)
813813

814814
# checks that we created a UUID1 with the proper timestamp
815815
assert new_dt == dt

tests/integration/cqlengine/model/test_model_io.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from uuid import uuid4, UUID
1717
import random
18-
from datetime import datetime, date, time
18+
from datetime import datetime, date, time, timezone
1919
from decimal import Decimal
2020
from operator import itemgetter
2121

@@ -197,13 +197,13 @@ class AllDatatypesModel(Model):
197197

198198
sync_table(AllDatatypesModel)
199199

200-
input = ['ascii', 2 ** 63 - 1, bytearray(b'hello world'), True, datetime.utcfromtimestamp(872835240),
200+
input = ['ascii', 2 ** 63 - 1, bytearray(b'hello world'), True, datetime.fromtimestamp(872835240, tz=timezone.utc).replace(tzinfo=None),
201201
Decimal('12.3E+7'), 2.39, 3.4028234663852886e+38, '123.123.123.123', 2147483647, 'text',
202202
UUID('FE2B4360-28C6-11E2-81C1-0800200C9A66'), UUID('067e6162-3b6f-4ae2-a171-2470b63dff00'),
203203
int(str(2147483647) + '000')]
204204

205205
AllDatatypesModel.create(id=0, a='ascii', b=2 ** 63 - 1, c=bytearray(b'hello world'), d=True,
206-
e=datetime.utcfromtimestamp(872835240), f=Decimal('12.3E+7'), g=2.39,
206+
e=datetime.fromtimestamp(872835240, tz=timezone.utc), f=Decimal('12.3E+7'), g=2.39,
207207
h=3.4028234663852886e+38, i='123.123.123.123', j=2147483647, k='text',
208208
l=UUID('FE2B4360-28C6-11E2-81C1-0800200C9A66'),
209209
m=UUID('067e6162-3b6f-4ae2-a171-2470b63dff00'), n=int(str(2147483647) + '000'),

tests/integration/cqlengine/model/test_udts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
import unittest
1515

16-
from datetime import datetime, date, time
16+
from datetime import datetime, date, time, timezone
1717
from decimal import Decimal
1818
from unittest.mock import Mock
1919
from uuid import UUID, uuid4
@@ -272,7 +272,7 @@ def test_can_insert_udts_with_all_datatypes(self):
272272
self.addCleanup(drop_table, AllDatatypesModel)
273273

274274
input = AllDatatypes(a='ascii', b=2 ** 63 - 1, c=bytearray(b'hello world'), d=True,
275-
e=datetime.utcfromtimestamp(872835240), f=Decimal('12.3E+7'), g=2.39,
275+
e=datetime.fromtimestamp(872835240, tz=timezone.utc).replace(tzinfo=None), f=Decimal('12.3E+7'), g=2.39,
276276
h=3.4028234663852886e+38, i='123.123.123.123', j=2147483647, k='text',
277277
l=UUID('FE2B4360-28C6-11E2-81C1-0800200C9A66'),
278278
m=UUID('067e6162-3b6f-4ae2-a171-2470b63dff00'), n=int(str(2147483647) + '000'))

tests/unit/cython/types_testhelper.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def test_datetype(assert_equal):
3838
cdef BytesIOReader reader
3939
cdef Buffer buf
4040

41-
dt = datetime.datetime.utcfromtimestamp(timestamp)
41+
dt = datetime.datetime.fromtimestamp(timestamp, tz=datetime.timezone.utc)
4242

4343
bytes = io.BytesIO()
4444
write_value(bytes, DateType.serialize(dt, 0))
@@ -52,7 +52,7 @@ def test_datetype(assert_equal):
5252
# deserialize
5353
# epoc
5454
expected = 0
55-
assert_equal(deserialize(expected), datetime.datetime.utcfromtimestamp(expected))
55+
assert_equal(deserialize(expected), datetime.datetime.fromtimestamp(expected, tz=datetime.timezone.utc).replace(tzinfo=None))
5656

5757
# beyond 32b
5858
expected = 2 ** 33

tests/unit/test_types.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def test_empty_value(self):
201201

202202
def test_datetype(self):
203203
now_time_seconds = time.time()
204-
now_datetime = datetime.datetime.utcfromtimestamp(now_time_seconds)
204+
now_datetime = datetime.datetime.fromtimestamp(now_time_seconds, tz=datetime.timezone.utc)
205205

206206
# Cassandra timestamps in millis
207207
now_timestamp = now_time_seconds * 1e3
@@ -212,23 +212,23 @@ def test_datetype(self):
212212
# deserialize
213213
# epoc
214214
expected = 0
215-
self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected))
215+
self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.fromtimestamp(expected, tz=datetime.timezone.utc).replace(tzinfo=None))
216216

217217
# beyond 32b
218218
expected = 2 ** 33
219-
self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(2242, 3, 16, 12, 56, 32))
219+
self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(2242, 3, 16, 12, 56, 32, tzinfo=datetime.timezone.utc).replace(tzinfo=None))
220220

221221
# less than epoc (PYTHON-119)
222222
expected = -770172256
223-
self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(1945, 8, 5, 23, 15, 44))
223+
self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(1945, 8, 5, 23, 15, 44, tzinfo=datetime.timezone.utc).replace(tzinfo=None))
224224

225225
# work around rounding difference among Python versions (PYTHON-230)
226226
expected = 1424817268.274
227-
self.assertEqual(DateType.deserialize(int64_pack(int(1000 * expected)), 0), datetime.datetime(2015, 2, 24, 22, 34, 28, 274000))
227+
self.assertEqual(DateType.deserialize(int64_pack(int(1000 * expected)), 0), datetime.datetime(2015, 2, 24, 22, 34, 28, 274000, tzinfo=datetime.timezone.utc).replace(tzinfo=None))
228228

229229
# Large date overflow (PYTHON-452)
230230
expected = 2177403010.123
231-
self.assertEqual(DateType.deserialize(int64_pack(int(1000 * expected)), 0), datetime.datetime(2038, 12, 31, 10, 10, 10, 123000))
231+
self.assertEqual(DateType.deserialize(int64_pack(int(1000 * expected)), 0), datetime.datetime(2038, 12, 31, 10, 10, 10, 123000, tzinfo=datetime.timezone.utc).replace(tzinfo=None))
232232

233233
def test_collection_null_support(self):
234234
"""

0 commit comments

Comments
 (0)