-
Notifications
You must be signed in to change notification settings - Fork 784
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
PyO3 incorrectly converts local to UTC times on FixedOffset conversion #3267
Comments
@Psykopear @pickfire Do you have the bandwidth to look into this? |
FWIW I think the fix here is to change these two |
@grantslatton Care to create a pull request? |
Hi @grantslatton @adamreichold . So, it's been a long time since that was merged, I need some time to go through the PRs and discussion to remember what was the reasoning behind that, but I think the UTC conversion was not accidental, see this comment (and in general the discussion in the PR. It's a long one, but I think we talked about almost every detail of the code in there): There might still be a bug in the conversion, but reading that, I'm not sure changing from Anyway if you are in a rush to merge #3269 , I trust pyo3 maintainers and if they are ok with the fix, I'm ok with it too, and if I find problems with it in the future I'll open a new issue to discuss this. |
I don't think there is any particular pressure here and I would prefer if we take the time to resolve this properly. More eyes make bugs shallower. |
Ok, I just noticed the other issue (#3266 ), and there is some more to extract from the previous discussion to properly answer that too. I'll try to find some time for this between this week and the next, but if I don't, please ping me again. It's important to get this right, and I'd like to share the output of the weeks we (me, the other authors, the reviewers) spent on the previous PRs because a lot of valuable insights was gathered at the time. Luckily everything's written down, but I understand that trying to find the important info in that discussion might be complicated if you weren't participating. I'd love to see proper timezone info support, but as usual when you deal with timezones (and python's timezones handling), getting it right is always harder than it looks. |
Yes, no pressure, as we have already implemented all of these fixes in our code manually, but it would be nice to move that code from our codebase to a fully supported replacement in pyo3 itself :) I think converting to UTC is not necessarily wrong, but if you do, you need to change the fixed offset timezone to always be UTC (i.e. offset 0) too. 0400+0200 is unambiguously equal to 0200+0000, so converting to UTC is lossless if you convert both the left and right side (time and offset). The code before was just mutating the time but not the offset. The test I provided proves this is a buggy (but was a hidden bug because going That said, there are maybe some philosophical reasons to prefer not always converting to UTC. If a user does |
Yes that's one of the things I really like about the way PyO3 is organized. I reimplemented the chrono integration in my own codebase after I found a bug in the original crate we were using, before deciding to try to upstream it (and with all the eyes on the review, including the author of the original crate, it did come up way better than my first implementation 😄 ). You need to create several newtypes and reimplement PyO3 traits on them, but you usually end up doing it for other reasons anyway.
Uh, I see now, and yep this looks like a bug. I'll look into the test and comment on the PR when I get to a checkpoint for what I'm working at.
Yep, I'd obviously rather have the exact same representation when passing a datetime object from Python->Rust->Python. I think there was a reason behind this, and it was not accidental. But as I said it's been some time, so I might be confusing this with something we did elsewhere in the integration, that's why I want to double check the discussion before saying more. |
Ok, sorry for delaying this, I misinterpreted the problem at first, but I reviewed the PR, and it's good for me, the issue is there and the PR fixes it. I added a comment there, the only concern I have is about the |
Bug Description
When converting to/from
datetime.datetime
/DateTime<FixedOffset>
, PyO3 is usingDateTime::naive_utc
andDateTime::from_utc
-- I believe these should benaive_local
andfrom_local
, respectively.Steps to Reproduce
This test fails with
I think the bug probably went unnoticed because it exists on both the
IntoPy
andFromPy
paths so cancels itself out when you go to and from, but you can induce it by creating dates in native python as shown above.Backtrace
No response
Your operating system and version
OS X Ventura 13.4
Your Python version (
python --version
)3.11
Your Rust version (
rustc --version
)1.70
Your PyO3 version
0.19
How did you install python? Did you use a virtualenv?
brew, no
Additional Info
No response
The text was updated successfully, but these errors were encountered: