[C++-sig] What is the best way to have a C++ function return a python datetime.date object or a python datetime.datetime object?

Holger.Joukl at LBBW.de Holger.Joukl at LBBW.de
Tue Jul 7 03:29:23 EDT 2020


Hi,

> I have a date and a datetime object in C++ and I would like to add the following methods:
>        toPyDate()
>        toPyDatetime()
>        fromPyDate()
>        fromPyDatetime()
>
> I would like to ‘bolt-on’ these methods to my C++ classes in the BOOST_PYTHON_MODULE interface layer.
>
> I am trying to avoid additional python wrappers around my boost python library.
>
> Can this be done without additional python?   What is the best/cleanest way to do this?

Without having tried it:

You should probably look at the Python datetime C-API and use it to construct DateTime objects,
then return these from your methods:

https://docs.python.org/3/c-api/datetime.html

Returning PyObjects should allow boost.python to work its magic.

The alternative being auto-magic conversion between your C++ date/time classes and Python
DateTime objects, e.g. along the lines of

https://www.boost.org/doc/libs/1_73_0/libs/python/doc/html/faq/how_can_i_automatically_convert_.html
https://stackoverflow.com/a/16109416

Best regards,
Holger






Landesbank Baden-Wuerttemberg
Anstalt des oeffentlichen Rechts
Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz
HRA 12704
Amtsgericht Stuttgart
HRA 4356, HRA 104 440
Amtsgericht Mannheim
HRA 40687
Amtsgericht Mainz

Die LBBW verarbeitet gemaess Erfordernissen der DSGVO Ihre personenbezogenen Daten.
Informationen finden Sie unter https://www.lbbw.de/datenschutz.


More information about the Cplusplus-sig mailing list