Skip to content

Timezone is handled wrong #2388

@HerrHofrat

Description

@HerrHofrat

What happened?

I was running into an issue when I pass a time object without timezone information into adapi.run_daily. my timezone is Europe/Vienna - so I'm currently 2h ahead GMT. However, scheduler would schedule the callbacks 55min lates - so for example instead of 23:00 it would run on 23:55.
I think I found the bug in the recently added timezone handling in adapi.run_daily:

                if start.tzinfo is None:
                    start = start.replace(tzinfo=self.AD.tz)
                date = await self.date()
                start = dt.datetime.combine(date, start)

With that code the time is calulated based on LMT not GMT

I wrote simple test script to highlight the issue:

>>> from datetime import datetime
>>> import pytz
>>> tz = pytz.timezone("Europe/Vienna")
>>> time = datetime.strptime("23:00", '%H:%M').time()
>>> time = time.replace(tzinfo = tz)
>>> date = datetime.now().date()
>>> datetime.combine(date, time)
datetime.datetime(2025, 6, 27, 23, 0, tzinfo=<DstTzInfo 'Europe/Vienna' LMT+1:05:00 STD>)

I refactored the code a bit to use localize from pytz:

>>> from datetime import datetime
>>> import pytz
>>> tz = pytz.timezone("Europe/Vienna")
>>> time = datetime.strptime("23:00", '%H:%M').time()
>>> date = datetime.now().date()
>>> start = datetime.combine(date, time)
>>> tz.localize(start)
datetime.datetime(2025, 6, 27, 23, 0, tzinfo=<DstTzInfo 'Europe/Vienna' CEST+2:00:00 DST>)

so it seems that combining date and time before doing tz.localize gives the correct result, while time.replace(tzinfo=tz) before combining date and time results in a wrong time.

Therefore I think that the code in adapi.run_daily needs to be refactored to something like this:

                has_no_tzinfo = start.tzinfo is None
                date = await self.date()
                start = dt.datetime.combine(date, start) 
                if has_no_tzinfo:
                    start = self.AD.tz.localize(start)

Let me know what you think,
Thanks, Roman

Version

4.5.11

Installation type

Docker container

Relevant log output

Relevant code in the app or config file that caused the issue

Anything else?

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions