Python: Usage of PyDateTime_FromTimestamp

坚强是说给别人听的谎言 提交于 2019-12-08 00:33:21

问题


I'm working on a python c-extension and want to create an instance of python datetime object with a unix timestamp.

On the documentation site ( http://docs.python.org/c-api/datetime.html ) I found the function PyDateTime_FromTimestamp() which returns a new reference based on an input parameter.

The description is as follows: Create and return a new datetime.datetime object given an argument tuple suitable for passing to datetime.datetime.fromtimestamp().

I tried out to call the function with a PyFloat_Object but the function always returns NULL (even if I simply put in 0).

Does somebody have an example how I have to call the function or can give a hint what kind of parameter tuple is required to get it work?

Thanks!


回答1:


Thank you Ignacio! Sometimes small hints make the solution - here the full working example:

static double doubleValue = 1314761451;  
PyObject *floatObj = NULL;  
PyObject *timeTuple = NULL;  
PyObject *dateTime = NULL;  
floatObj = PyFloat_FromDouble(doubleValue);  
timeTuple = Py_BuildValue("(O)", floatObj);  
dateTime = PyDateTime_FromTimestamp(timeTuple);


来源:https://stackoverflow.com/questions/7236364/python-usage-of-pydatetime-fromtimestamp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!