Repository navigation
Fix instance() ignoring the tz argument for aware datetimes - #1049
Open
developer-rpai wants to merge 1 commit into
Open
developer-rpai wants to merge 1 commit into
developer-rpai wants to merge 1 commit into
Conversation
The tz argument of pendulum.instance() and DateTime.instance() was silently ignored whenever the given datetime already carried a timezone: the datetime's own tzinfo always won and no conversion happened. An explicitly passed timezone now takes precedence. Aware datetimes are converted to it, naive ones are interpreted in it as before. When no timezone is passed, the previous behavior is unchanged: aware datetimes keep their timezone and naive ones default to UTC. DateTime.utcfromtimestamp(), fromordinal() and combine() relied on passing tz=None to stay naive, so they now build their result directly instead of going through instance().
Wkhanex
reviewed
Oct 12, 2026
There was a problem hiding this comment.
I checked the changed DateTime.instance method with a datetime adapter and ZoneInfo conversion hooks. Four conversions covering both folds of New York’s 2024 fall-back hour to UTC and Brussels preserved the original timestamp. Default-aware input retained its timezone, explicit-zone naive input retained its wall time, and default-naive input used UTC. Those controls support the distinction between converting aware inputs and interpreting naive inputs. Consider a DST-fold regression alongside the new ordinary-date tests. I ran these checks on Python 3.12; isolated method checks, not the full Pendulum parser/timezone suite.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #841.
pendulum.instance(dt, tz="EST")silently ignored thetzargument wheneverdtalready carried a timezone:DateTime.instance()unconditionally preferreddt.tzinfo, so no conversion ever happened and the returned value kept the original timezone.This makes an explicitly passed timezone take precedence:
tzparameter ofinstance()(and ofDateTime.instance()/Time.instance()) now defaults toNone.Two follow-on adjustments were needed:
DateTime.utcfromtimestamp(),DateTime.fromordinal()andDateTime.combine()used to passtz=Nonethroughinstance()to get a naive result, so they now build their result directly. Their behavior is unchanged (atzinfogiven tocombine()now wins over the time's own tzinfo, matching the standard library).tzoption toinstance()with a UTC default, which would have turned an unspecified option into an explicit conversion request; it now forwards the option only when one was actually given.Tests: the full suite passes (1875 passed, 3 skipped), including new regression tests in
tests/test_main.pycovering conversion for bothpendulum.instance()andDateTime.instance(), the reporter's pendulum DateTime case, and naive datetimes with an explicit timezone.