PyO3 / pyo3

Rust bindings for the Python interpreter
https://pyo3.rs
Apache License 2.0
12.03k stars 742 forks source link

Add utcfromtimestamp() to DateTime bindings #375

Closed thedrow closed 5 years ago

thedrow commented 5 years ago

utcfromtimestamp is a very common method. I believe it should be part of the API.

pganssle commented 5 years ago

I strongly disagree with this for a number of reasons, the foremost of which is that you should not be using utcfromtimestamp even in Python.

For one thing, it's not part of the C API - we have bindings for everything that's in the C API, and utcfromtimestamp is not part of that.

For another, the utcfromtimestamp and utcnow are both problematic in that they represent a specific time in a specific time zone using a naive datetime object, but many methods on datetime treat a naive datetime as being in your local time. Those methods are essentially broken because they are inconsistent with either model for datetime - the one where a bare datetime refers to an abstract datetime and the one where a bare datetime refers to the datetime in local time. If you want a datetime from a timestamp in UTC, you should use datetime.fromtimestamp(ts, datetime.timezone.utc).

I believe we have the FFI bindings for the datetime.timezone.utc singleton in Python 3.7+, but we still need safe Rust wrappers for it.