agronholm / apscheduler

Task scheduling library for Python
MIT License
5.93k stars 690 forks source link

Infinite loop in CalendarIntervalTrigger with UTC timezone #897

Closed unights closed 2 months ago

unights commented 2 months ago

Things to check first

Version

4.0.0a4

What happened?

Time with UTC timezone will be always not equal to other time.

from datetime import time
from zoneinfo import ZoneInfo

utctime = time(0, 0, tzinfo=ZoneInfo('UTC'))

a = time(0, 0)
b = time(0, 0, tzinfo=ZoneInfo("Asia/Shanghai"))
c = time(0, 0, tzinfo=ZoneInfo("Asia/Tokyo"))

print(a == utctime)  # False
print(a == b == c)   # True

So the condition here will always be False

https://github.com/agronholm/apscheduler/blob/3e42a1bd73da947634ea94724e5221a43ad7ce0c/src/apscheduler/triggers/calendarinterval.py#L121

It should be call .timetz() to get the time part.

How can we reproduce the bug?

from apscheduler.triggers.calendarinterval import CalendarIntervalTrigger

trigger = CalendarIntervalTrigger(days=1, timezone="UTC")

print(trigger.next())

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\.venv\Lib\site-packages\apscheduler\triggers\calendarinterval.py", line 109, in next
    next_time = datetime.fromtimestamp(timestamp, self.timezone)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 22] Invalid argument