- Notifications
You must be signed in to change notification settings - Fork5.7k
Description
PR#1485 madetelegram.utils.helpers.from_timestamp
return a naïvedatetime.datetime
object in UTC, but this created an inconsistency with the rest of the repository in the handling of naïvedatetime.datetime
s, where it is assumed that these objects representlocal time. In fact, there is a direct inconsistency betweenfrom_timestamp
andto_timestamp
helpers, since the latter still assumes that thedatetime
object is in local time.
See#1497 (comment) (point3.
) for extended discussion.
I've already fixed the issue in#1497 (and added tests), but should I make a separate PR to isolate the change?
Steps to reproduce
Go to a place with nonzero UTC offset. I live in CEST (GMT +2).
Construct a
telegram.Message
from a JSON data dictionary. This is a message I sent to a test bot of mine (stripped of the optional fields), as received from an HTTP request to Telegram:importtelegramastgmessage_json= {'message_id':462,'from': {'id':41214001,'is_bot':False,'first_name':'Paolo', },'chat': {'id':41214001,'type':'private' },'date':1567596574,}original_timestamp=message_json['date']bot=tg.Bot('000:not_a_token')message=tg.Message.de_json(message_json,bot=bot)
Re-form the
Message
's JSONdict
and extract the timestamp:restored_timestamp=message.to_dict()['date']
Check that the timestamps are the same:
assertrestored_timestamp==original_timestamp, \f"Timestamps differ by{restored_timestamp-original_timestamp} seconds"
Expected behaviour
The timestamps should be the same.
Actual behaviour
The timestamps differ by the local UTC offset (reversed):
AssertionError: Timestamps differ by -7200 seconds
When theMessage
is constructed, thedatetime
object it gets represents UTC time, but when it gets re-converted to a timestamp, that same object is assumed to be in local time, which in my case is7200
seconds ahead of UTC, so the resulting timestamp is7200
seconds behind the original.
Configuration
Operating System: Windows
Version of Python, python-telegram-bot & dependencies:
$ python -m telegram
python-telegram-bot 12.0.0 (v12.0.0-0-gc84e21d)certifi 2019.03.09future 0.17.1Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]