有没有办法将人类可读的时间“09:41:43”转换为某种可比较的格式?
我想要的是function timeGreater(time1, time2)
,满足以下断言
assert(true == timeGreater("09:41:43", "09:00:42"))
assert(false == timeGreater("12:55:43", "19:00:43")))
有没有办法将人类可读的时间“09:41:43”转换为某种可比较的格式?
我想要的是function timeGreater(time1, time2)
,满足以下断言
assert(true == timeGreater("09:41:43", "09:00:42"))
assert(false == timeGreater("12:55:43", "19:00:43")))
似乎一个简单的字符串比较可能就足够了(假设时间有效):
function timeGreater(a, b) return a > b end
assert(true == timeGreater("09:41:43", "09:00:42"))
assert(false == timeGreater("12:55:43", "19:00:43"))
将您的时间转换为秒应该可以。下面的代码可能会起作用,LUA 不是我的强项!
function stime(s)
local pattern = "(%d+):(%d+):(%d+)"
local hours, minutes, seconds = string.match(s, pattern)
return (hours*3600)+(minutes*60)+seconds
end
function timeGreater(a, b)
return stime(a) > stime(b)
end