1

In the C API structure in datetime.h, I see the following type objects:

PyTypeObject *DateType;
PyTypeObject *DateTimeType;
PyTypeObject *TimeType;
PyTypeObject *DeltaType;
PyTypeObject *TZInfoType;

But there is no:

TimeZoneType

I need to instantiate a datetime.timezone object to make a datetime.datetime object aware.

I would have called:

PyObject *tzobj = PyObject_CallObject(PyDateTimeAPI->TimeZoneType, argList);

and then the replace(tzinfo=...) method on datetime object

But I can't do that.

What is the alternative to construct a aware datetime object from C/C++ ?

MMM
  • 910
  • 1
  • 9
  • 25

2 Answers2

1

The type you're looking for is defined in Lib/datetime.py

class timezone(tzinfo):
    [...]

so you might need to do a from datetime import timezone to get this type.

static PyObject*
load_timezone_type(void) {
    PyObject *dt = PyImport_ImportModule("datetime");
    if (!dt) return NULL;
    PyObject *tz = PyObject_GetAttrString(dt, "timezone");
    Py_DECREF(dt);
    return tz;
}
tynn
  • 38,113
  • 8
  • 108
  • 143
0

The timezone class has not been exposed in datetime C API yet. See issue 10381.

Alexander Belopolsky
  • 2,228
  • 10
  • 26