fix: ensure next_fire in TimeTrigger returns proper next time #1575
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request Type
Description
This one's a bit weird, so I'll explain it through an example.
Let's say, on a bot's device, the time is currently 11:00 PM (23:00) EST on 2023-11-27 (YYYY-MM-DD). EST has a time difference of -5 hours from UTC, making this 4:00 AM UTC on the next day, 2023-11-28. Now, say, we create a task that looks like so:
Naturally, this should result in the task, if started, to not trigger until 10:59 PM (22:59) EST, or 3:59 AM UTC, the next day (2023-11-28). However, this will not happen - what will happen is that the console will have an endless spam of
test
until midnight EST. But why is this the case?The offender of this issue is in
TimeTrigger
'snext_fire
, so let's break it down.On the first run of this program,
now
is 11:00 PM (23:00) EST on 2023-11-27. Thus,target
is 3:59 AM UTC on 2023-11-27. Because thetarget
's timezone is UTC,target
gets converted into the equivalent time in (well, in this case) EST - 10:59 PM (22:59) EST on 2023-11-26.When first starting the bot, we can assume
self.last_call_time
is essentiallynow
- it'll maybe be a couple of seconds behind, but that doesn't matter. Now, ourtarget
is indeed less than the time it is right now - as an attempt to fix this, the code adds a day totarget
, making it 10:59 PM (22:59) EST on 2023-11-27.Note, though, that the current time is 11:00 PM (23:00) EST on 2023-11-27, meaning the
target
is still behindnow
when that should never happen, breaking the rest of the task's code's assumptions and causing that loop.Now, this all happens because
target
, when being converted from UTC to local/native time, goes back a day, when the code incorrectly assumes that it'll stay on the same day. This PR fixes that by ensuring that the newtarget
, after the time conversions, is set back to the current date as specified bynow
.Changes
target
has the same date (though not time) asnow
inTimeTrigger
'snext_fire()
.Related Issues
Test Scenarios
See the description.
Python Compatibility
3.10.x
3.11.x
Checklist
pre-commit
code linter over all edited files