-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Description
PR #1485 made telegram.utils.helpers.from_timestamp return a naïve datetime.datetime object in UTC, but this created an inconsistency with the rest of the repository in the handling of naïve datetime.datetimes, where it is assumed that these objects represent local time. In fact, there is a direct inconsistency between from_timestamp and to_timestamp helpers, since the latter still assumes that the datetime object is in local time.
See #1497 (comment) (point 3.) 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.Messagefrom 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:import telegram as tg message_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 JSONdictand extract the timestamp:restored_timestamp = message.to_dict()['date']
-
Check that the timestamps are the same:
assert restored_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 the Message is constructed, the datetime 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 is 7200 seconds ahead of UTC, so the resulting timestamp is 7200 seconds behind the original.
Configuration
Details
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.09
future 0.17.1
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]