Title: Inconsistent use of UTC/local naïve datetimes · Issue #1505 · python-telegram-bot/python-telegram-bot · GitHub
Open Graph Title: Inconsistent use of UTC/local naïve datetimes · Issue #1505 · python-telegram-bot/python-telegram-bot
X Title: Inconsistent use of UTC/local naïve datetimes · Issue #1505 · python-telegram-bot/python-telegram-bot
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 t...
Open Graph 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 dateti...
X 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 dateti...
Opengraph URL: https://github.com/python-telegram-bot/python-telegram-bot/issues/1505
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Inconsistent use of UTC/local naïve datetimes","articleBody":"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.datetime`s, 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.\r\n\r\nSee [#1497 (comment)](https://github.com/python-telegram-bot/python-telegram-bot/pull/1497#issuecomment-527256274) (point `3.`) for extended discussion.\r\n\r\nI've already fixed the issue in #1497 (and added tests), but should I make a separate PR to isolate the change?\r\n\r\n\r\n### Steps to reproduce\r\n\r\n0. Go to a place with nonzero UTC offset. I live in CEST (GMT +2).\r\n\r\n1. 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:\r\n\r\n ```python\r\n import telegram as tg\r\n \r\n message_json = {\r\n 'message_id': 462,\r\n 'from': {\r\n 'id': 41214001,\r\n 'is_bot': False,\r\n 'first_name': 'Paolo',\r\n },\r\n 'chat': {\r\n 'id': 41214001,\r\n 'type': 'private'\r\n },\r\n 'date': 1567596574,\r\n }\r\n \r\n original_timestamp = message_json['date']\r\n\r\n bot = tg.Bot('000:not_a_token')\r\n message = tg.Message.de_json(message_json, bot=bot)\r\n ```\r\n\r\n2. Re-form the `Message`'s JSON `dict` and extract the timestamp:\r\n ```python\r\n restored_timestamp = message.to_dict()['date']\r\n ```\r\n\r\n3. Check that the timestamps are the same:\r\n ```python\r\n assert restored_timestamp == original_timestamp, \\\r\n f\"Timestamps differ by {restored_timestamp - original_timestamp} seconds\"\r\n ```\r\n\r\n### Expected behaviour\r\nThe timestamps should be the same.\r\n\r\n### Actual behaviour\r\nThe timestamps differ by the local UTC offset (reversed):\r\n\r\n```text\r\nAssertionError: Timestamps differ by -7200 seconds\r\n```\r\n\r\nWhen 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.\r\n\r\n### Configuration\r\n\u003cdetails>\r\n\r\n**Operating System:** Windows\r\n\r\n\r\n**Version of Python, python-telegram-bot & dependencies:**\r\n\r\n``$ python -m telegram``\r\n\r\n```\r\npython-telegram-bot 12.0.0 (v12.0.0-0-gc84e21d)\r\ncertifi 2019.03.09\r\nfuture 0.17.1\r\nPython 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]\r\n```\r\n\r\n\u003c/details>\r\n","author":{"url":"https://github.com/plammens","@type":"Person","name":"plammens"},"datePublished":"2019-09-04T12:20:33Z","url":"https://github.com/python-telegram-bot/python-telegram-bot/issues/1505"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:681b9814-f561-e9f0-b3a8-862056e7fb63 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DED2:9E08E:157129E:1AC502B:6B0258A2 |
| html-safe-nonce | 4d35bd135607d73213334ad126d379cb36817867e273c71226b7a4a5339def91 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERUQyOjlFMDhFOjE1NzEyOUU6MUFDNTAyQjo2QjAyNThBMiIsInZpc2l0b3JfaWQiOiI3MTgyNDgzMzYxODgxMzU1ODYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 5304cabbc9830406f8ea2550d79b178655f9c570abb1f663ce40200dcee875f8 |
| hovercard-subject-tag | issue:489125782 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python-telegram-bot/python-telegram-bot/1505/issue_layout |
| twitter:image | https://opengraph.githubassets.com/416df4137a4aeddec8ea24c47b2e8339a9ffabcdcf3a74b9bcd54e237098e797/python-telegram-bot/python-telegram-bot/issues/1505 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/416df4137a4aeddec8ea24c47b2e8339a9ffabcdcf3a74b9bcd54e237098e797/python-telegram-bot/python-telegram-bot/issues/1505 |
| og:image:alt | 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 dateti... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | plammens |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python-telegram-bot/python-telegram-bot git https://github.com/python-telegram-bot/python-telegram-bot.git |
| octolytics-dimension-user_id | 16178365 |
| octolytics-dimension-user_login | python-telegram-bot |
| octolytics-dimension-repository_id | 38696925 |
| octolytics-dimension-repository_nwo | python-telegram-bot/python-telegram-bot |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 38696925 |
| octolytics-dimension-repository_network_root_nwo | python-telegram-bot/python-telegram-bot |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width