如何使用 Cython 从 time.h 调用时间?

Ada*_*rak 2 python time cython

我尝试直接使用 Cython 而不是 Python 加载 time.himport time但它不起作用。

我得到的只是一个错误

Call with wrong number of arguments (expected 1, got 0)
Run Code Online (Sandbox Code Playgroud)

用下面的代码

cdef extern from "time.h" nogil:
    ctypedef int time_t
    time_t time(time_t*)

def test():
    cdef int ts
    ts = time()


    return ts
Run Code Online (Sandbox Code Playgroud)

Cannot assign type 'long' to 'time_t *'
Run Code Online (Sandbox Code Playgroud)

用下面的代码

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
Run Code Online (Sandbox Code Playgroud)

有了数学日志我可以简单地做

cdef extern from "math.h":
    double log10(double x)
Run Code Online (Sandbox Code Playgroud)

怎么随着时间的推移就不可能了呢?

Syl*_*oux 5

参数 totime是要填充的值的地址(即:“指针”)或 NULLtime_t

去引用man 2 time

time_t time(time_t *t);

[...]

如果t非NULL,则返回值也存储在t指向的内存中。

某些标准函数会返回一个值并(可能)在提供的地址中存储相同的值,这是一个奇怪的现象。0作为参数传递是完全安全的,因为在大多数体系结构中NULL相当于((void*)0)。在这种情况下,time将仅返回结果,而不会尝试将其存储在提供的地址中。