python: use datetime.fromtimestamp() instead of datetime.utcfromtimestamp()

utcfromtimestamp() is not recommended by the official documentation,
because many methods, including timestamp(), work incorrectly with
"naive" datetimes returned by utcfromtimestamp().
This commit is contained in:
link2xt
2021-10-30 22:15:01 +00:00
parent 54e79409e6
commit 3e60ee9d3e
4 changed files with 13 additions and 12 deletions

View File

@@ -3,7 +3,7 @@
import mimetypes import mimetypes
import calendar import calendar
import json import json
from datetime import datetime from datetime import datetime, timezone
import os import os
from .cutil import as_dc_charpointer, from_dc_charpointer, iter_array from .cutil import as_dc_charpointer, from_dc_charpointer, iter_array
from .capi import lib, ffi from .capi import lib, ffi
@@ -512,8 +512,9 @@ class Chat(object):
latitude=lib.dc_array_get_latitude(dc_array, i), latitude=lib.dc_array_get_latitude(dc_array, i),
longitude=lib.dc_array_get_longitude(dc_array, i), longitude=lib.dc_array_get_longitude(dc_array, i),
accuracy=lib.dc_array_get_accuracy(dc_array, i), accuracy=lib.dc_array_get_accuracy(dc_array, i),
timestamp=datetime.utcfromtimestamp( timestamp=datetime.fromtimestamp(
lib.dc_array_get_timestamp(dc_array, i) lib.dc_array_get_timestamp(dc_array, i),
timezone.utc
), ),
marker=from_dc_charpointer(lib.dc_array_get_marker(dc_array, i)), marker=from_dc_charpointer(lib.dc_array_get_marker(dc_array, i)),
) )

View File

@@ -1,6 +1,6 @@
from .capi import lib from .capi import lib
from .capi import ffi from .capi import ffi
from datetime import datetime from datetime import datetime, timezone
def as_dc_charpointer(obj): def as_dc_charpointer(obj):
@@ -44,4 +44,4 @@ class DCLot:
ts = lib.dc_lot_get_timestamp(self._dc_lot) ts = lib.dc_lot_get_timestamp(self._dc_lot)
if ts == 0: if ts == 0:
return None return None
return datetime.utcfromtimestamp(ts) return datetime.fromtimestamp(ts, timezone.utc)

View File

@@ -6,7 +6,7 @@ from . import props
from .cutil import from_dc_charpointer, as_dc_charpointer from .cutil import from_dc_charpointer, as_dc_charpointer
from .capi import lib, ffi from .capi import lib, ffi
from . import const from . import const
from datetime import datetime from datetime import datetime, timezone
class Message(object): class Message(object):
@@ -170,7 +170,7 @@ class Message(object):
:returns: naive datetime.datetime() object. :returns: naive datetime.datetime() object.
""" """
ts = lib.dc_msg_get_timestamp(self._dc_msg) ts = lib.dc_msg_get_timestamp(self._dc_msg)
return datetime.utcfromtimestamp(ts) return datetime.fromtimestamp(ts, timezone.utc)
@props.with_doc @props.with_doc
def time_received(self): def time_received(self):
@@ -180,7 +180,7 @@ class Message(object):
""" """
ts = lib.dc_msg_get_received_timestamp(self._dc_msg) ts = lib.dc_msg_get_received_timestamp(self._dc_msg)
if ts: if ts:
return datetime.utcfromtimestamp(ts) return datetime.fromtimestamp(ts, timezone.utc)
@props.with_doc @props.with_doc
def ephemeral_timer(self): def ephemeral_timer(self):
@@ -200,7 +200,7 @@ class Message(object):
""" """
ts = lib.dc_msg_get_ephemeral_timestamp(self._dc_msg) ts = lib.dc_msg_get_ephemeral_timestamp(self._dc_msg)
if ts: if ts:
return datetime.utcfromtimestamp(ts) return datetime.fromtimestamp(ts, timezone.utc)
@property @property
def quoted_text(self): def quoted_text(self):

View File

@@ -10,7 +10,7 @@ from deltachat.tracker import ImexTracker
from deltachat.hookspec import account_hookimpl from deltachat.hookspec import account_hookimpl
from deltachat.capi import ffi, lib from deltachat.capi import ffi, lib
from deltachat.cutil import iter_array from deltachat.cutil import iter_array
from datetime import datetime, timedelta from datetime import datetime, timedelta, timezone
@pytest.mark.parametrize("msgtext,res", [ @pytest.mark.parametrize("msgtext,res", [
@@ -447,7 +447,7 @@ class TestOfflineChat:
contact1.create_chat().send_text("hello") contact1.create_chat().send_text("hello")
def test_chat_message_distinctions(self, ac1, chat1): def test_chat_message_distinctions(self, ac1, chat1):
past1s = datetime.utcnow() - timedelta(seconds=1) past1s = datetime.now(timezone.utc) - timedelta(seconds=1)
msg = chat1.send_text("msg1") msg = chat1.send_text("msg1")
ts = msg.time_sent ts = msg.time_sent
assert msg.time_received is None assert msg.time_received is None
@@ -2163,7 +2163,7 @@ class TestOnlineAccount:
break # DC is done with reading messages break # DC is done with reading messages
def test_send_receive_locations(self, acfactory, lp): def test_send_receive_locations(self, acfactory, lp):
now = datetime.utcnow() now = datetime.now(timezone.utc)
ac1, ac2 = acfactory.get_two_online_accounts() ac1, ac2 = acfactory.get_two_online_accounts()
lp.sec("ac1: create chat with ac2") lp.sec("ac1: create chat with ac2")