How to call time with time.h with Cython?

I am trying to load time.h directly using Cython instead of Python import time , but it does not work.

All I get is a mistake

 Call with wrong number of arguments (expected 1, got 0) 

with the following code

 cdef extern from "time.h" nogil: ctypedef int time_t time_t time(time_t*) def test(): cdef int ts ts = time() return ts 

and

 Cannot assign type 'long' to 'time_t *' 

with the following code

 cdef extern from "time.h" nogil: ctypedef int time_t time_t time(time_t*) def test(): cdef int ts ts = time(1) return ts 

with a math journal i can just do

 cdef extern from "math.h": double log10(double x) 

How is this possible with time ?

0
source share
2 answers

The time parameter is the address (that is: a "pointer") of the time_t value to fill in or NULL.

To quote man 2 time :

time_t time(time_t *t);

[...]

If t is not NULL, the return value is also stored in the memory pointed to by t.

The oddness of some standard functions returns both values ​​and (possibly) stores the same value in the provided address. It is absolutely safe to pass 0 as a parameter, since in most architectures NULL is equivalent to ((void*)0) . In this case, time will return the result and will not try to save it at the specified address.

+2
source

Go to NULL by time. You can also use the built-in libc.time:

 from libc.time cimport time,time_t cdef time_t t = time(NULL) print t 

which gives

 1471622065 
0
source

All Articles