Skip to content

fix: TypeError when passing timezone-aware datetime to until()#657

Open
koteshyelamati wants to merge 1 commit into
dbader:masterfrom
koteshyelamati:patch-1
Open

fix: TypeError when passing timezone-aware datetime to until()#657
koteshyelamati wants to merge 1 commit into
dbader:masterfrom
koteshyelamati:patch-1

Conversation

@koteshyelamati

Copy link
Copy Markdown

Bug

Passing a timezone-aware datetime to job.until() raises TypeError: can't compare offset-naive and offset-aware datetimes in two places:

  1. The past-time validation in until() compares self.cancel_after (aware) against datetime.datetime.now() (naive).
  2. _is_overdue() compares the naive when argument against the aware self.cancel_after.

Reproduction

import schedule
import datetime
from zoneinfo import ZoneInfo

tz = ZoneInfo('America/New_York')
future = datetime.datetime.now(tz) + datetime.timedelta(hours=1)

# Raises TypeError: can't compare offset-naive and offset-aware datetimes
schedule.every(10).minutes.until(future).do(lambda: None)

Fix

  • In until(): compare against datetime.datetime.now(tz=self.cancel_after.tzinfo) when cancel_after is timezone-aware.
  • In _is_overdue(): localize the naive when via astimezone() before comparing, when cancel_after carries tzinfo.

Passing a timezone-aware datetime to until() raises TypeError because
the validation check compared an aware cancel_after against a naive
datetime.datetime.now(), and _is_overdue() made the same mistake when
checking job.run() and _schedule_next_run().

Fix until() to call datetime.now(tz=cancel_after.tzinfo) when
cancel_after is timezone-aware, and fix _is_overdue() to localize the
native `when` argument before comparison when cancel_after carries tzinfo.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant