Skip to content

Commit

Permalink
Replace datetime.utcfromtimestamp by datetime.fromtimestamp
Browse files Browse the repository at this point in the history
It is depricated and to be removed soon.
  • Loading branch information
dkropachev committed Jan 4, 2025
1 parent d62eb38 commit 98d88d9
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions cassandra/cqlengine/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

from copy import deepcopy, copy
from datetime import date, datetime, timedelta
from datetime import date, datetime, timedelta, timezone
import logging
from uuid import UUID as _UUID

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

return datetime.utcfromtimestamp(value)
return datetime.fromtimestamp(value, timezone.utc)

def to_database(self, value):
value = super(DateTime, self).to_database(value)
Expand Down
2 changes: 1 addition & 1 deletion cassandra/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from cassandra import DriverException

DATETIME_EPOC = datetime.datetime(1970, 1, 1)
UTC_DATETIME_EPOC = datetime.datetime.utcfromtimestamp(0)
UTC_DATETIME_EPOC = datetime.datetime.fromtimestamp(0, datetime.timezone.utc)

_nan = float('nan')

Expand Down
6 changes: 3 additions & 3 deletions tests/integration/cqlengine/columns/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import unittest

import sys
from datetime import datetime, timedelta, date, tzinfo, time
from datetime import datetime, timedelta, date, tzinfo, time, timezone
from decimal import Decimal as D
from uuid import uuid4, uuid1
from packaging.version import Version
Expand Down Expand Up @@ -97,7 +97,7 @@ def test_datetime_timestamp(self):
dt_value = 1454520554
self.DatetimeTest.objects.create(test_id=5, created_at=dt_value)
dt2 = self.DatetimeTest.objects(test_id=5).first()
self.assertEqual(dt2.created_at, datetime.utcfromtimestamp(dt_value))
self.assertEqual(dt2.created_at, datetime.fromtimestamp(dt_value, timezone.utc))

def test_datetime_large(self):
dt_value = datetime(2038, 12, 31, 10, 10, 10, 123000)
Expand Down Expand Up @@ -809,7 +809,7 @@ def test_conversion_specific_date(self):
assert isinstance(uuid, UUID)

ts = (uuid.time - 0x01b21dd213814000) / 1e7 # back to a timestamp
new_dt = datetime.utcfromtimestamp(ts)
new_dt = datetime.fromtimestamp(ts, timezone.utc)

# checks that we created a UUID1 with the proper timestamp
assert new_dt == dt
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/cqlengine/model/test_model_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from uuid import uuid4, UUID
import random
from datetime import datetime, date, time
from datetime import datetime, date, time, timezone
from decimal import Decimal
from operator import itemgetter

Expand Down Expand Up @@ -200,13 +200,13 @@ class AllDatatypesModel(Model):

sync_table(AllDatatypesModel)

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

AllDatatypesModel.create(id=0, a='ascii', b=2 ** 63 - 1, c=bytearray(b'hello world'), d=True,
e=datetime.utcfromtimestamp(872835240), f=Decimal('12.3E+7'), g=2.39,
e=datetime.fromtimestamp(872835240, timezone.utc), f=Decimal('12.3E+7'), g=2.39,
h=3.4028234663852886e+38, i='123.123.123.123', j=2147483647, k='text',
l=UUID('FE2B4360-28C6-11E2-81C1-0800200C9A66'),
m=UUID('067e6162-3b6f-4ae2-a171-2470b63dff00'), n=int(str(2147483647) + '000'),
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/cqlengine/model/test_udts.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
import unittest

from datetime import datetime, date, time
from datetime import datetime, date, time, timezone
from decimal import Decimal
from mock import Mock
from uuid import UUID, uuid4
Expand Down Expand Up @@ -272,7 +272,7 @@ def test_can_insert_udts_with_all_datatypes(self):
self.addCleanup(drop_table, AllDatatypesModel)

input = AllDatatypes(a='ascii', b=2 ** 63 - 1, c=bytearray(b'hello world'), d=True,
e=datetime.utcfromtimestamp(872835240), f=Decimal('12.3E+7'), g=2.39,
e=datetime.fromtimestamp(872835240, timezone.utc), f=Decimal('12.3E+7'), g=2.39,
h=3.4028234663852886e+38, i='123.123.123.123', j=2147483647, k='text',
l=UUID('FE2B4360-28C6-11E2-81C1-0800200C9A66'),
m=UUID('067e6162-3b6f-4ae2-a171-2470b63dff00'), n=int(str(2147483647) + '000'))
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def test_empty_value(self):

def test_datetype(self):
now_time_seconds = time.time()
now_datetime = datetime.datetime.utcfromtimestamp(now_time_seconds)
now_datetime = datetime.datetime.fromtimestamp(now_time_seconds, datetime.timezone.utc)

# Cassandra timestamps in millis
now_timestamp = now_time_seconds * 1e3
Expand All @@ -211,7 +211,7 @@ def test_datetype(self):
# deserialize
# epoc
expected = 0
self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected))
self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.fromtimestamp(expected, datetime.timezone.utc))

# beyond 32b
expected = 2 ** 33
Expand Down

0 comments on commit 98d88d9

Please sign in to comment.