Python:PyDateTime_FromTimestamp的用法 [英] Python: Usage of PyDateTime_FromTimestamp

查看:852
本文介绍了Python:PyDateTime_FromTimestamp的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理python c-extension,并想创建一个带有unix时间戳的python datetime对象的实例.

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

在文档站点上( http://docs.python.org/c-api/datetime.html ),我发现了函数PyDateTime_FromTimestamp(),该函数会根据输入参数返回新的引用.

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.

说明如下: 创建并返回一个新的datetime.datetime对象,该对象具有适合传递给datetime.datetime.fromtimestamp()的参数元组.

我尝试使用PyFloat_Object调用该函数,但是该函数始终返回NULL(即使我只是简单地输入0).

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?

谢谢!

推荐答案

谢谢Ignacio!有时,一些小提示可以解决问题-这里是完整的工作示例:

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);

这篇关于Python:PyDateTime_FromTimestamp的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆