验证给定日期是否为昨天日期

我刚学习lua编程,现在正在kong上进行许可证验证。

我想用当前日期验证到期日期。

我该如何在lua脚本中进行验证。

点赞
用户7347992
用户7347992

我为你写了这个函数:

function verifyExpiration(expirationDate)
  local expirationTime = os.time(expirationDate)
  local currentTime = os.time()
  local result = false
 
  if (expirationTime < currentTime) then
    result = true
  end
  
  return result
end

它将返回:

  • true 如果已过期
  • false 如果没有过期

下面是一些使用示例:

> expT = {year=2018, month=1, day=1}
> verifyExpiration(expT)
> print(verifyExpiration(expT))
true
> expT = {year=2019, month=1, day=1}
> print(verifyExpiration(expT))
false
2018-05-21 17:01:44
用户4984564
用户4984564
-- 如果给定的时间已经过去,则返回 true。
function dateExpired(expirationTime)
  return os.difftime(os.time(), expirationTime) < 0
end

请注意,expirationTimeos.time()os.date() 所返回的时间值。如果您将日期保存为数据表,请先通过 os.time() 进行转换: dateExpired(os.time{year=2018, month=5, day=22})

2018-05-22 07:15:48