Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Usage of PyDateTime_FromTimestamp

57 views
Skip to first unread message

Andreas

unread,
Aug 30, 2011, 4:42:29 PM8/30/11
to pytho...@python.org
Hi,

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

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!

MRAB

unread,
Aug 30, 2011, 5:49:04 PM8/30/11
to pytho...@python.org
The key phrase is "argument tuple". The arguments passed to a Python
call are always a tuple, not PyFloat_Object.

You can build a tuple from the PyFloat_Object using:

Py_BuildValue("(O)", float_object)

The "(O)" says to build a tuple ("(...)") containing a single object
("O").

Andreas

unread,
Aug 30, 2011, 11:39:33 PM8/30/11
to pytho...@python.org
Am 30.08.2011 23:49, schrieb MRAB:

> The key phrase is "argument tuple". The arguments passed to a Python
> call are always a tuple, not PyFloat_Object.
>
> You can build a tuple from the PyFloat_Object using:
>
> Py_BuildValue("(O)", float_object)
>
> The "(O)" says to build a tuple ("(...)") containing a single object
> ("O").

Thank you very much! That solved my problem.
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);


MRAB

unread,
Aug 31, 2011, 12:41:55 PM8/31/11
to pytho...@python.org
On 31/08/2011 04:39, Andreas wrote:
> Am 30.08.2011 23:49, schrieb MRAB:
>
>> The key phrase is "argument tuple". The arguments passed to a Python
>> call are always a tuple, not PyFloat_Object.
>>
>> You can build a tuple from the PyFloat_Object using:
>>
>> Py_BuildValue("(O)", float_object)
>>
>> The "(O)" says to build a tuple ("(...)") containing a single object
>> ("O").
>
Some other points:

Py_BuildValue defaults to building a tuple if the format string
contains multiple items, eg "OO" means the same as "(OO)". In your
case, there is only one, so the (...) is required.

The object returned will have a reference count of 1, and any Python
object passed to the function will have its reference count incremented.

> Thank you very much! That solved my problem.
> 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);
>

PyFloat_FromDouble returns an object with a reference count of 1 and
Py_BuildValue increments that reference count, so you should probably
be decrementing it afterwards, otherwise you'll have a memory leak. The
usual rule is that you create it, use it, then 'decref' it.

Also, you should be doing some error-checking, seeing if Py_BuildValue,
etc, is returning NULL, indicating an error.

0 new messages