diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-05-03 09:14:05 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-05-03 09:14:05 +0100 |
commit | 8082c4bc4b8d305ae9f1df52829f9e87f3469564 (patch) | |
tree | 13f2033810e6d3cbd0fb1a1c229524dbf7ed2986 /src/lua/lua_util.c | |
parent | ee5ce1e90bca75985fac7553f091725bd06290c4 (diff) | |
download | rspamd-8082c4bc4b8d305ae9f1df52829f9e87f3469564.tar.gz rspamd-8082c4bc4b8d305ae9f1df52829f9e87f3469564.zip |
[Feature] Add some time manipulation functions for lua APi
Diffstat (limited to 'src/lua/lua_util.c')
-rw-r--r-- | src/lua/lua_util.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 059b47cfc..1506676ea 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -232,6 +232,21 @@ LUA_FUNCTION_DEF (util, strequal_caseless); LUA_FUNCTION_DEF (util, get_ticks); /*** + * @function util.get_time() + * Returns current time as unix time in floating point representation + * @return {number} number of seconds since 01.01.1970 + */ +LUA_FUNCTION_DEF (util, get_time); + +/*** + * @function util.time_to_string(seconds) + * Converts time from Unix time to HTTP date format + * @param {number} seconds unix timestamp + * @return {string} date as HTTP date + */ +LUA_FUNCTION_DEF (util, time_to_string); + +/*** * @function util.stat(fname) * Performs stat(2) on a specified filepath and returns table of values * @@ -326,6 +341,8 @@ static const struct luaL_reg utillib_f[] = { LUA_INTERFACE_DEF (util, strcasecmp_ascii), LUA_INTERFACE_DEF (util, strequal_caseless), LUA_INTERFACE_DEF (util, get_ticks), + LUA_INTERFACE_DEF (util, get_time), + LUA_INTERFACE_DEF (util, time_to_string), LUA_INTERFACE_DEF (util, stat), LUA_INTERFACE_DEF (util, unlink), LUA_INTERFACE_DEF (util, lock_file), @@ -1152,6 +1169,49 @@ lua_util_get_ticks (lua_State *L) } static gint +lua_util_get_time (lua_State *L) +{ + gdouble seconds; + struct timeval tv; + + if (gettimeofday (&tv, NULL) == 0) { + seconds = tv_to_double (&tv); + } + else { + seconds = time (NULL); + } + + lua_pushnumber (L, seconds); + + return 1; +} + +static gint +lua_util_time_to_string (lua_State *L) +{ + gdouble seconds; + struct timeval tv; + char timebuf[128]; + + if (lua_isnumber (L, 1)) { + seconds = lua_tonumber (L, 1); + } + else { + if (gettimeofday (&tv, NULL) == 0) { + seconds = tv_to_double (&tv); + } + else { + seconds = time (NULL); + } + } + + rspamd_http_date_format (timebuf, sizeof (timebuf), seconds); + lua_pushstring (L, timebuf); + + return 1; +} + +static gint lua_util_stat (lua_State *L) { const gchar *fpath; |