TGStat
TGStat
Type to search
Advanced channel search
  • flag English
    Site language
    flag Russian flag English flag Uzbek
  • Sign In
  • Catalog
    Channels and groups catalog Regional compilations Thematic compilations Платные каналы Search for channels
    Add a channel/group
  • Ratings
    Rating of channels Rating of groups Posts rating
    Ratings of brands and people
  • Analytics
  • Search by posts
  • Telegram monitoring
  • Promotion
    Advertising through Yandex Business Advertising in channels through TGStat Agency Advertising on TGStat.ru website
Python tests

6 Sep, 17:14

Open in Telegram Share Report

00:57
🐍 Python считает эти даты равными. Хотя между ними целый час

В ночь перевода часов время 02:30 наступает дважды:


from datetime import datetime, UTC
from zoneinfo import ZoneInfo

tz = ZoneInfo("Europe/Berlin")

a = datetime(2026, 10, 25, 2, 30, tzinfo=tz, fold=0)
b = datetime(2026, 10, 25, 2, 30, tzinfo=tz, fold=1)

print(a)
# 2026-10-25 02:30:00+02:00

print(b)
# 2026-10-25 02:30:00+01:00


Это разные моменты:


a = 00:30 UTC
b = 01:30 UTC


Но Python выдаёт:


a == b # True
b - a # 0:00:00
len({a, b}) # 1


Причина: если у двух datetime один объект tzinfo, при сравнении Python игнорирует смещение и fold.

Из-за этого дедупликация может принять два платежа, заказа или события за одно.

Для сравнения реальных моментов переводите время в UTC:


a_utc = a.astimezone(UTC)
b_utc = b.astimezone(UTC)

a_utc == b_utc
# False

(b_utc - a_utc).total_seconds()
# 3600.0


Используйте astimezone(UTC). Метод replace(tzinfo=UTC) только заменяет метку и не пересчитывает время.

https://docs.python.org/3/library/datetime.html

https://peps.python.org/pep-0495/

439 0 1
Catalog
Channels and groups catalog Channels compilations Search for channels Add a channel/group
Ratings
Rating of Telegram channels Rating of Telegram groups Posts rating Ratings of brands and people
API
API statistics Search API of posts API Callback
Our channels
@TGStat @TGStat_Chat @telepulse @TGStatAPI
Read
Академия TGStat Telegram Research 2019 Telegram Research 2021 Telegram Research 2023
Contacts
Справочный центр Support Email Jobs
Miscellaneous
Terms and conditions Privacy policy Public offer
Our bots
@TGStat_Bot @SearcheeBot @TGAlertsBot @tg_analytics_bot @TGStatChatBot