diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-02-23 14:13:58 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-02-23 14:13:58 +0000 |
commit | 4fda9686da8300e8f187ae784819f8eb040846e5 (patch) | |
tree | b8539fc73d2fe96523b865d7b0320d17486dddaa /lualib/lua_util.lua | |
parent | 13885fec3a2a84a49687f2d50ce7cbe8723b2af2 (diff) | |
download | rspamd-4fda9686da8300e8f187ae784819f8eb040846e5.tar.gz rspamd-4fda9686da8300e8f187ae784819f8eb040846e5.zip |
[Minor] Move parse_time_interval to lua_util
Diffstat (limited to 'lualib/lua_util.lua')
-rw-r--r-- | lualib/lua_util.lua | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index f7e92937d..729e73a5f 100644 --- a/lualib/lua_util.lua +++ b/lualib/lua_util.lua @@ -215,4 +215,45 @@ end exports.disable_module = disable_module +local function parse_time_interval(str) + local function parse_time_suffix(s) + if s == 's' then + return 1 + elseif s == 'm' then + return 60 + elseif s == 'h' then + return 3600 + elseif s == 'd' then + return 86400 + elseif s == 'y' then + return 365 * 86400; + end + end + + local lpeg = require "lpeg" + + local digit = lpeg.R("09") + local parser = {} + parser.integer = + (lpeg.S("+-") ^ -1) * + (digit ^ 1) + parser.fractional = + (lpeg.P(".") ) * + (digit ^ 1) + parser.number = + (parser.integer * + (parser.fractional ^ -1)) + + (lpeg.S("+-") * parser.fractional) + parser.time = lpeg.Cf(lpeg.Cc(1) * + (parser.number / tonumber) * + ((lpeg.S("smhdy") / parse_time_suffix) ^ -1), + function (acc, val) return acc * val end) + + local t = lpeg.match(parser.time, str) + + return t +end + +exports.parse_time_interval = parse_time_interval + return exports |