Python:PyDateTime_FromTimestamp 的用法

发布于 2024-12-01 18:31:38 字数 486 浏览 2 评论 0原文

我正在开发一个 python c 扩展,并且想要创建一个带有 unix 时间戳的 python datetime 对象的实例。

在文档网站上 ( http://docs.python.org/c-api/datetime. html )我找到了函数 PyDateTime_FromTimestamp() ,它根据输入参数返回一个新引用。

描述如下: 创建并返回一个新的 datetime.datetime 对象,给定一个适合传递给 datetime.datetime.fromtimestamp() 的参数元组。

我尝试使用 PyFloat_Object 调用该函数,但该函数始终返回 NULL (甚至如果我简单地输入 0)。

有人有一个例子,我如何调用该函数,或者可以提示需要什么样的参数元组才能使其工作?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晰传感 2024-12-08 18:31:38

谢谢伊格纳西奥!有时,小提示就能解决问题 - 这里是完整的工作示例:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文