匹配即将到来的日期 In Lua

Match an approaching date In Lua

我正在寻找有关 Lua 脚本的一些帮助。本质上,我希望匹配即将到来的日期 X 今天之前的分钟数。在下面的示例中,我使用了 9000 分钟。

alarm.get ()

message = "Certificate Expiry Warning - Do something"
SUPPKEY = "Certificate Expiry"
SUBSYS = "1.1"
SOURCE = "SERVERNAME"

--local pattern = "(%d-%m-%Y)"

local t = os.date('*t'); -- get current date and time

print(os.date("%d-%m-%Y")); --Prints todays date

t.min = t.min - 9000; -- subtract 9000 minutes

--print(os.date("%Y-%m-%d %H:%m:%S", os.time(t))); --Original Script

print(os.date("%d-%m-%Y", os.time(t))); --Prints alerting date

if string.match ~=t.min --Match string

--if string.match(a.message, pattern)

--then print (al.message)

then print ("We have a match")

--then nimbus.alarm (1, message , SUPPKEY , SUBSYS , SOURCE) --Sends alert

else print ("Everything is fine") --Postive, no alert

--else print (al.message)

end

alarm.get 抓取一行文本,如下所示:

DOMAIN\USERNAME,网络服务器 (WebServer),13/01/2017 09:13,13/01/2019,COMPANY_NAME, HOSTNAME_FQDN,网站

所以上面显示的行作为 a.message 变量传递,我希望将粗体突出显示的日期与今天的日期相匹配,减去 9000 分钟。

注释掉的部分只是我在测试不同的东西。

我不确定我是否理解了这个问题,但从我的角度来看,您似乎在尝试做两件事:

  1. 以 DD/MM/YYYY.
  2. 格式检索当前时间减去 9000 分钟
  3. 将这次时间与您的程序从文件中读取的时间进行比较,并在两个日期相等时执行某些操作。

这是我的示例代码:

-- Settings
local ALLOWED_AGE = 9000 -- In minutes

-- Input line (for testing only)
local inputstr = "DOMAIN\USERNAME,Web Server (WebServer),13/01/2017 09:13,13/01/2019,COMPANY_NAME,HOSTNAME_FQDN,SITE"

-- Separate line into 7 variables by token ","
local path, server, time, date, company_name, hostname, site = string.match(inputstr, "([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+)")

-- Check, if the line is ok (not necessary, but should be here to handle possible errors)
-- Also note, some additional checks should be here (eg. regex to match DD/MM/YYYY format)
if date == nil then
   print("Error reading line: "..inputstr)
end

-- Get current time minus 9000 minutes (in format DD/MM/YYYY)
local target_date = os.date("%d/%m/%Y", os.time() - ALLOWED_AGE * 60)

-- Printing what we got (for testing purposes)
print("Target date: "..target_date..", Input date: "..date)

-- Testing the match
if target_date == date then
   print("Dates are matched!")
else
   print("Dates are not matched!")
end

虽然我不确定,但您是否不应该检查您的情况 "one date is bigger/smaller then the other"。

那么上面的代码应该修改成这样:

-- Extract day, month and year from date in format DD/MM/YYYY
local d, m, y = string.match(date, "([^/]+)/([^/]+)/([^/]+)")
-- Note I'm adding one day, so the certificate will actually expire day after it's "valid until" date.
local valid_until = os.time({year = y, month = m, day = d + 1})
local expire_time = os.time() - ALLOWED_AGE * 60 -- All certificates older than this should expire.

-- Printing what we got (for testing purposes)
print("Expire time: "..expire_time..", Cert valid until: "..valid_until)

-- Is expired?
if valid_until <= expire_time then
   print("Oops! Certificate expired.")
else
   print("Certificate date is valid.")
end