Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Inconsistent use of UTC/local naïve datetimes #1505

Labels
@plammens

Description

@plammens

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.datetimes, 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

  1. Go to a place with nonzero UTC offset. I live in CEST (GMT +2).

  2. Construct atelegram.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)
  3. Re-form theMessage's JSONdict and extract the timestamp:

    restored_timestamp=message.to_dict()['date']
  4. 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)]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp