From: Vsevolod Stakhov Date: Tue, 3 May 2016 08:14:05 +0000 (+0100) Subject: [Feature] Add some time manipulation functions for lua APi X-Git-Tag: 1.3.0~576 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8082c4bc4b8d305ae9f1df52829f9e87f3469564;p=thirdparty%2Frspamd.git [Feature] Add some time manipulation functions for lua APi --- diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 059b47cfc7..1506676eac 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -231,6 +231,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), @@ -1151,6 +1168,49 @@ lua_util_get_ticks (lua_State *L) return 1; } +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) {