有没有办法将人类可读时间“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")))
答案 0 :(得分:4)
似乎简单的字符串比较可能就足够了(假设时间有效):
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"))
答案 1 :(得分:3)
将时间转换为秒应该有效。下面的代码可能有用,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