[Python] How to add/subtract time in Python C API?

Discussão em 'Python' iniciado por Stack, Agosto 14, 2025.

  1. Stack

    Stack Membro Participativo

    There are many tutorials and examples out there on how to perform date-arithmetic from Python. But I cannot find anything for the Python C API...

    My C function, callable from Python, operates on two dates: start and finish. If start is not explicitly specified, it must be understood as one day before the finish.

    How do I do this?

    /* Attempt converting the finish-date, if not a date already */
    if (!PyDate_Check(finishO)) {

    o = PyDate_FromTimestamp(finishO);
    if (o == NULL)
    return PyErr_Format(PyExc_ValueError,
    "%R is not a valid date", finishO);
    finishO = o;
    }

    /* If start-date is not specified, assume one day prior the finish date */
    if (startO == NULL) {
    PyObject *oneDay = PyDelta_FromDSU(1, 0, 0);
    /* Derive startO from finishO by applying the one-day delta somehow */
    }

    Continue reading...

Compartilhe esta Página