[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: lcron
- From: Lev <leventelist@...>
- Date: Wed, 25 May 2016 02:02:35 +0200
Hi list,
So I've written a small Lua library that has similar funcionality as cron(8).
The question is how can I share this with the comunity? Shall I create a
github repo? Is there any central database for these?
Thanks,
Lev
--
73 de HA5OGL
Op.: Levente
local lcron = {
__VERSION = 'lcron.lua 1.1.0',
__DESCRIPTION = "Levente's cron for lua",
__URL = 'http://levente.logonex.eu/',
__LICENSE = [[
Copyright (c) 2014-2016 Levente Kovacs
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
}
local function eval_field (num, pattern)
local lp, rp, l, r
if pattern==nil then
return true
end
if pattern=="*" then
return true
end
local ll = 0
local subpattern
local stepp
local step
while true do
step=nil
lr = string.find(pattern, ",", ll)
if lr ~=nil then
subpattern = string.sub(pattern, ll, lr-1)
else
subpattern = string.sub(pattern, ll)
end
stepp=string.find(subpattern, "/", ll)
if stepp~=nil then
step=tonumber(string.sub(subpattern, stepp+1))
subpattern = string.sub(subpattern, 0, stepp-1)
end
local val=nil
if type(subpattern)=='number' then
val=subpattern
elseif type(subpattern)=='string' then
val=tonumber(subpattern)
end
if val~=nil then
if val==num then
return true
elseif step~=nil and ((num-val)%step)==0 then
return true
end
end
lp,rp=string.find (subpattern, "-")
if lp~=nil then
l=tonumber(string.sub(subpattern, 1, lp-1))
r=tonumber(string.sub(subpattern, rp+1))
if type(l)=='nil' then return false end
if type(r)=='nil' then return false end
if l<=num and num<=r then
if step==nil then
return true
elseif ((num-l)%step)==0 then
return true
end
end
end
if lr==nil then
break
end
ll=lr+1
end
return false
end
function lcron.at(tab)
local bdt=os.date("*t")
if type(tab)~='table' then
return false
end
--Convert wday to ISO8601 weekday format (Monday=1, Sunday=7), so one can specify weekend by '6-7'
local wday=bdt.wday-1
if wday==0 then
wday=7
end
if not eval_field(bdt.year, tab.year) then
return false
elseif not eval_field(bdt.month, tab.month) then
return false
elseif not eval_field(bdt.day, tab.dom) then
return false
elseif not eval_field(wday, tab.dow) then
return false
elseif not eval_field(bdt.hour, tab.hour) then
return false
elseif not eval_field(bdt.min, tab.min) then
return false
end
return true
end
function lcron.cron(tab)
local lr
local ll
local i=0
local t={}
ll=0
while true do
lr = string.find(tab, "%s+", ll)
if lr ~=nil then
field = string.sub(tab, ll, lr-1)
else
field = string.sub(tab, ll)
end
if i==0 then
t.min=field
elseif i==1 then
t.hour=field
elseif i==2 then
t.dom=field
elseif i==3 then
t.month=field
elseif i==4 then
t.dow=field
end
if lr==nil then
break
end
ll=lr+1
i=i+1
end
t.year="*"
return lcron.at(t)
end
return lcron
local lcron = require 'lcron'
local data = {year="2017,2010-2030/2, 2019", month=5, dow='*', dom='25', hour="1-22", min='*'}
if lcron.at(data) then
print ("Match! :-)")
else
print ("No match :-(")
end
if lcron.cron("* 7 25 5 1-6") then
print ("Match! :-)")
else
print ("No match :-(")
end