我需要一个简单的函数将秒转换为人类可以读取以表示倒数的时间字符串。我可以通过非常直观的数学计算获得所需的结果,但不够优雅。
然后我注意到有一个名为os.date的函数,但是我无法获得想要的结果,调用有什么问题,这是我的代码:
#!usr/bin/env lua
local function getDate1(sec)
local days = math.floor(sec / 86400)
local hours = math.floor((sec % 86400) / 3600)
local minutes = math.floor((sec % 3600) / 60)
local seconds = math.floor(sec % 60)
return days, hours, minutes, seconds
end
local function getDate2(sec)
local date = os.date("*t", sec)
return date["day"], date["hour"], date["min"], date["sec"]
end
local function printDate(d, h, m, s)
print(string.format("%02d %02d:%02d:%02d", d, h, m, s))
end
function main()
printDate(getDate1(999999))
printDate(getDate2(999999))
end
main()
11 13:46:39
12 21:46:39
local function getDate2(sec)
local date = os.date("!*t", sec)
return date["day"]-1, date["hour"], date["min"], date["sec"]
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句