#include #include #include #include unsigned long long get_time() { struct timeval tv; gettimeofday(&tv, NULL); unsigned long long unix_us = tv.tv_sec * 1000000 + tv.tv_usec; return unix_us; } int tapi_unix(lua_State* L) { lua_pushnumber(L, get_time() / 1000000.0d); return 1; } int tapi_unix_us(lua_State* L) { lua_pushinteger(L, get_time()); return 1; } int tapi_ms_now(lua_State* L) { struct timeval tv; gettimeofday(&tv, NULL); lua_pushinteger(L, tv.tv_usec/1000); return 1; } int tapi_sec_now(lua_State* L) { struct timeval tv; gettimeofday(&tv, NULL); lua_pushnumber(L, ((double)tv.tv_usec)/1000000.0d+((double)(tv.tv_sec % 60))); return 1; } int tapi_min_now(lua_State* L) { struct timeval tv; gettimeofday(&tv, NULL); lua_pushnumber(L, ((double)tv.tv_usec)/60000000.0d+((double)(tv.tv_sec % (60 * 60)))/60.0d); return 1; } int tapi_unix_struct(lua_State* L) { struct timeval tv; gettimeofday(&tv, NULL); lua_createtable(L, 0, 2); lua_pushstring(L, "sec"); lua_pushinteger(L, tv.tv_sec); lua_settable(L, -3); lua_pushstring(L, "usec"); lua_pushinteger(L, tv.tv_usec); lua_settable(L, -3); return 1; } int tapi_usleep(lua_State* L) { lua_pushinteger(L, usleep((useconds_t)lua_tonumber(L, 1))); return 1; } int luaopen_tapi(lua_State* L) { lua_createtable(L, 0, 3); lua_pushstring(L, "unix"); lua_pushcfunction(L, tapi_unix); lua_settable(L, -3); lua_pushstring(L, "unix_us"); lua_pushcfunction(L, tapi_unix_us); lua_settable(L, -3); lua_pushstring(L, "ms_now"); lua_pushcfunction(L, tapi_ms_now); lua_settable(L, -3); lua_pushstring(L, "sec_now"); lua_pushcfunction(L, tapi_sec_now); lua_settable(L, -3); lua_pushstring(L, "min_now"); lua_pushcfunction(L, tapi_min_now); lua_settable(L, -3); lua_pushstring(L, "unix_struct"); lua_pushcfunction(L, tapi_unix_struct); lua_settable(L, -3); lua_pushstring(L, "usleep"); lua_pushcfunction(L, tapi_usleep); lua_settable(L, -3); return 1; }