Lua - Current time in milliseconds

后端 未结 10 921
自闭症患者
自闭症患者 2020-12-01 01:56

Is there a common way to get the current time in or with milliseconds?

There is os.time(), but it only provides full seconds.

相关标签:
10条回答
  • 2020-12-01 02:36

    Kevlar is correct.

    An alternative to a custom DLL is Lua Alien

    0 讨论(0)
  • 2020-12-01 02:37

    If you're using OpenResty then it provides for in-built millisecond time accuracy through the use of its ngx.now() function. Although if you want fine grained millisecond accuracy then you may need to call ngx.update_time() first. Or if you want to go one step further...

    If you are using luajit enabled environment, such as OpenResty, then you can also use ffi to access C based time functions such as gettimeofday() e.g: (Note: The pcall check for the existence of struct timeval is only necessary if you're running it repeatedly e.g. via content_by_lua_file in OpenResty - without it you run into errors such as attempt to redefine 'timeval')

    if pcall(ffi.typeof, "struct timeval") then
            -- check if already defined.
    else
            -- undefined! let's define it!
            ffi.cdef[[
               typedef struct timeval {
                    long tv_sec;
                    long tv_usec;
               } timeval;
    
            int gettimeofday(struct timeval* t, void* tzp);
    ]]
    end
    local gettimeofday_struct = ffi.new("struct timeval")
    local function gettimeofday()
            ffi.C.gettimeofday(gettimeofday_struct, nil)
            return tonumber(gettimeofday_struct.tv_sec) * 1000000 + tonumber(gettimeofday_struct.tv_usec)
    end
    

    Then the new lua gettimeofday() function can be called from lua to provide the clock time to microsecond level accuracy.

    Indeed, one could take a similar approaching using clock_gettime() to obtain nanosecond accuracy.

    0 讨论(0)
  • 2020-12-01 02:39

    If you're using lua with nginx/openresty you could use ngx.now() which returns a float with millisecond precision

    0 讨论(0)
  • 2020-12-01 02:42

    Get current time in milliseconds.

    os.time()

    os.time()
    return sec // only
    

    posix.clock_gettime(clk)

    https://luaposix.github.io/luaposix/modules/posix.time.html#clock_gettime

    require'posix'.clock_gettime(0)
    return sec, nsec
    

    linux/time.h // man clock_gettime

    /*
     * The IDs of the various system clocks (for POSIX.1b interval timers):
     */
    #define CLOCK_REALTIME                  0
    #define CLOCK_MONOTONIC                 1
    #define CLOCK_PROCESS_CPUTIME_ID        2
    #define CLOCK_THREAD_CPUTIME_ID         3
    #define CLOCK_MONOTONIC_RAW             4
    #define CLOCK_REALTIME_COARSE           5
    #define CLOCK_MONOTONIC_COARSE          6
    

    socket.gettime()

    http://w3.impa.br/~diego/software/luasocket/socket.html#gettime

    require'socket'.gettime()
    return sec.xxx
    

    as waqas says


    compare & test

    get_millisecond.lua

    local posix=require'posix'
    local socket=require'socket'
    
    for i=1,3 do
        print( os.time() )
        print( posix.clock_gettime(0) )
        print( socket.gettime() )
        print''
        posix.nanosleep(0, 1) -- sec, nsec
    end
    

    output

    lua get_millisecond.lua
    1490186718
    1490186718      268570540
    1490186718.2686
    
    1490186718
    1490186718      268662191
    1490186718.2687
    
    1490186718
    1490186718      268782765
    1490186718.2688
    
    0 讨论(0)
提交回复
热议问题