How to call time from time.h with Cython?

后端 未结 2 1303
囚心锁ツ
囚心锁ツ 2021-01-16 06:26

I am trying to load time.h directly with Cython instead of Python\'s import time but it doesn\'t work.

All I get is an error

Call with w         


        
相关标签:
2条回答
  • 2021-01-16 06:38

    Pass in NULL to time. Also you can use the builtin libc.time:

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

    which gives

    1471622065
    
    0 讨论(0)
  • 2021-01-16 06:50

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

    To quote man 2 time:

    time_t time(time_t *t);

    [...]

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

    It is an oddity of some standard functions to both return a value and (possibly) store the same value in a provided address. It is perfectly safe to pass 0 as parameter as in most architecture NULL is equivalent to ((void*)0). In that case, time will only return the result, and will not attempt to store it in the provided address.

    0 讨论(0)
提交回复
热议问题