python datetime to timestamp_Python:PyDateTime_FromTimestamp的用法

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!

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值