From ab9035acd261fa79dd95138b2d25e7990490f1c4 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 12 Nov 2020 15:24:33 +0100 Subject: [PATCH] Make large dates wrap around Closes #1629. --- telethon/tl/tlobject.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/telethon/tl/tlobject.py b/telethon/tl/tlobject.py index cd563857..4b94e00f 100644 --- a/telethon/tl/tlobject.py +++ b/telethon/tl/tlobject.py @@ -15,7 +15,11 @@ def _datetime_to_timestamp(dt): dt = dt.replace(tzinfo=timezone.utc) # We use .total_seconds() method instead of simply dt.timestamp(), # because on Windows the latter raises OSError on datetimes ~< datetime(1970,1,1) - return int((dt - _EPOCH).total_seconds()) + secs = int((dt - _EPOCH).total_seconds()) + # Make sure it's a valid signed 32 bit integer, as used by Telegram. + # This does make very large dates wrap around, but it's the best we + # can do with Telegram's limitations. + return struct.unpack('i', struct.pack('I', secs & 0xffffffff))[0] def _json_default(value):