Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to own .fold when calling .replace() #414

Merged

Conversation

tomage
Copy link
Contributor

@tomage tomage commented Oct 16, 2019

After doing a lot of experiments, it seemed to me that there was a problem when calling .replace() during DST off-transition (i.e. in the immediate hour after the transition). Reported issue here: #415.

So a seemingly innocent code like this:

    my_new_dt = my_old_dt.replace(microseconds=0)

would change the datetime in more ways than simply setting microseconds to zero.

This patch makes it so that when .replace() is called, it respects the .fold flag set on the datetime itself, which seems to resolve the issue.

Now, I'm no expert in timezones myself, so this might definitely be the exact wrong approach - but my gut feeling told me that it seemed odd to just default to the pendulum.POST_TRANSITION rule anytime we do replace, so I figured utilizing the flag seemed the right way to go about it. Seemed more symmetrical to the surrounding code too.

I added one more test module, to explicitly test out the .replace() function. Wasn't sure where best to fit it, but can easily accommodate a different place for it. As it happened, one of the "fluent" tests also broke - one that is dong a .replace(tzinfo=...) call. So I suppose I could have put my replace tests there, but wasn't sure what "fluent" meant in this context. So I ended up adding a few more cases for those .replace(tzinfo= tests, to get solid coverage surrounding DST transition points. Not quite convinced if those tests are what you had in mind when you wrote the original test, so hoping I'm not deviating from the intended design.

Well - looking forwards to your feedback - feel free to push back on any/all of this.

@tomage tomage force-pushed the fix-replace-after-dst-transition branch from b984da8 to 95ad44b Compare July 5, 2020 22:53
@@ -139,18 +139,41 @@ def test_fluid_at_with_transition():
assert 0 == new.second


def test_replace_tzinfo():
d = pendulum.datetime(2016, 7, 2, 0, 41, 20)
def test_replace_tzinfo_dls_off():
Copy link
Collaborator

@sdispater sdispater Jul 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why dls here and not dst?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right - I think I started off calling it "dls" (daylight-savings), but later saw that "dst" was a more common way to name it.

I'll rename to "dst".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with a fixup commit (i.e. I can squash into one commit before merge if you like, unless if you want to just squash+merge)

@sdispater sdispater merged commit 36e8726 into python-pendulum:master Jul 10, 2020
@sdispater
Copy link
Collaborator

Thanks!

@sdispater sdispater mentioned this pull request Jul 13, 2020
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.

2 participants