DateTime Objects

Various date and time objects are supplied by the datetime module. Before using any of these functions, the header file datetime.h must be included in your source (note that this is not included by Python.h), and the macro PyDateTime_IMPORT must be invoked, usually as part of the module initialisation function. The macro puts a pointer to a C structure into a static variable, PyDateTimeAPI, that is used by the following macros.

PyDateTime_IMPORT()

Import the datetime C API.

On success, populate the PyDateTimeAPI pointer. On failure, set PyDateTimeAPI to NULL and set an exception. The caller must check if an error occurred via PyErr_Occurred():

PyDateTime_IMPORT;
if (PyErr_Occurred()) { /* cleanup */ }

Warning

This is not compatible with subinterpreters.

type PyDateTime_CAPI

Structure containing the fields for the datetime C API.

The fields of this structure are private and subject to change.

Do not use this directly; prefer PyDateTime_* APIs instead.

PyDateTime_CAPI *PyDateTimeAPI

Dynamically allocated object containing the datetime C API.

This variable is only available once PyDateTime_IMPORT succeeds.

type PyDateTime_Date

This subtype of PyObject represents a Python date object.

type PyDateTime_DateTime

This subtype of PyObject represents a Python datetime object.

type PyDateTime_Time

This subtype of PyObject represents a Python time object.

type PyDateTime_Delta

This subtype of PyObject represents the difference between two datetime values.

PyTypeObject PyDateTime_DateType

This instance of PyTypeObject represents the Python date type; it is the same object as datetime.date in the Python layer.

PyTypeObject PyDateTime_DateTimeType

This instance of PyTypeObject represents the Python datetime type; it is the same object as datetime.datetime in the Python layer.

PyTypeObject PyDateTime_TimeType

This instance of PyTypeObject represents the Python time type; it is the same object as datetime.time in the Python layer.

PyTypeObject PyDateTime_DeltaType

This instance of PyTypeObject represents the Python type for the difference between two datetime values; it is the same object as datetime.timedelta in the Python layer.