1

I'm currently working with the cpython datetime api attempting to write pybind11 bindings for Howard Hinnant's date library zoned_time objects and to do so I must perform bidirectional conversions between Howard Hinnant's TimeZone and the cpython PyDateTime_TZInfo.

The PyDateTime_CAPI in cpython defines a number of operations to create the PyDateTime_* objects; however, it defines only the type for TZInfo and no methods to create these objects. The implementation of the api defines a very basic implementation of a timezone; however, this is not exposed through the API. Ultimately, in order to have proper operability on the timezone objects, there would need to be some C++ implementation of the TZInfo interface. The issue I see here is that the API code relies call_tzinfo_method which is making a call to a method assumed to be implemented on a derived python class as opposed to an implemented method on an interface. From what I can see here, there is no way to implement TZInfo objects in C++ which ultimately would make it impossible to properly bind timezone aware datetime objects. Is there some way around this or is this simply impossible in the current cpython implementation?

mcguip
  • 5,947
  • 5
  • 25
  • 32
  • In Python 3.7, [datetime.timezone](https://docs.python.org/3.7/library/datetime.html#timezone-objects) objects are exposed through the C-API. They are a subclass/implementation of tzinfo right in core Python. This likely is what you're looking for, but it's only Python 3.7+. I'd also be interested in knowing of an independent C/C++ implementation of tzinfo, so that I could use it for Python < 3.7. – movermeyer May 14 '18 at 14:17
  • @movermeyer, presumably one could just use Howard Hinnant's date library as it will be part of the standard lib. I would think the python datetime lib should eventually use this. I have wrapped this library using pybind11 and use it as my tzinfo implementation – mcguip May 14 '18 at 15:21
  • @movermeyer is your wrapper code on Github? – Damian Nov 27 '18 at 04:53
  • @Damian, I assume your question is directed at me given I was the only one to mention wrappers. Given that there is now support for zoned times in the cpython (as of python 3.7), the pybind11 wrappers for Howard's library are largely not needed. I think the best course would be to look at creating your own conversions within pybind from native python types into HH's types. – mcguip Nov 27 '18 at 10:13
  • @mcguip yes, sorry the Q was for you. I am will on Py 3.6 so might still need it before we can upgrade. – Damian Nov 28 '18 at 08:12
  • @Damian, there is an implementation of timezone objects written in C, if that's what you are looking for. see https://stackoverflow.com/questions/50336229/are-there-any-datetime-tzinfo-implementations-in-c – movermeyer Nov 28 '18 at 12:01

1 Answers1

1

TZInfo objects can be created in the CPython API from version 3.7 using PyTimeZone_FromOffsetAndName and PyTimeZone_FromOffset

mcguip
  • 5,947
  • 5
  • 25
  • 32
  • If you're interested in an independent C implementation of timezone objects ([as I was](https://stackoverflow.com/questions/50336229/are-there-any-datetime-tzinfo-implementations-in-c)), the pendulum library [made their own](https://github.com/sdispater/pendulum/blob/3698fcbe39a42d5df8147e1ddc06160fe03ed000/pendulum/parsing/_iso8601.c#L259). This can be useful for supporting versions of Python < 3.7. – movermeyer Jun 28 '18 at 12:37