How do I best parse a date string in the Python C API?
Question: Unfortunately there seems to be no `PyDateTime_FromString()` (equivalent to
e.g. `PyFloat_FromString()`) in the Python C API. Has anybody figured out what
would be the ideal workaround if I have to parse a date string into a
`datetime.datetime` type?


Answer: If
[`PyDateTime_FromTimestamp`](https://docs.python.org/2/c-api/datetime.html#c.PyDateTime_FromTimestamp)
does not suffice, you can just call any Python method like
[`strptime`](https://docs.python.org/2/library/datetime.html#datetime.datetime.strptime)
directly:

    
    
    PyObject *datetimeType = (PyObject *) PyDateTimeAPI->DateTimeType;
    PyObject *datetime = PyObject_CallMethod(datetimeType, "strptime", "ss",
                                                "11-11-11 11:11:11", //date_string
                                                "%y-%m-%d %H:%M:%S"); //format
    

But keep in mind, that `PyDateTimeAPI->DateTimeType` is not part of the
documented API, so it could break eventually any time in the future. I've
tested this with Python versions 2.7.9 and 3.4.2 only. Instead you should do
the equivalent of `from datetime import datetime` in your C code.

