如何从Lua中自1970年1月1日以来经过的秒数中获取日期(月份、日、年)?

我目前有这个:

function Date(T)
    if not T then return "never" end
    local Z = math.floor(T/86400)+719468
    local ERA = math.floor(Z/146097)
    local DOE = math.floor(Z-ERA*146097)
    local YOE = math.floor((DOE-DOE/1460+DOE/36524-DOE/146096)/365)
    local DOY = DOE-math.floor((365*YOE+YOE/4-YOE/100))
    local MP = math.floor((5*DOY+2)/153)
    local M = math.floor(MP+(MP<10 and 3 or -9))
    return ({"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"})[M].." "..math.ceil(DOY-(153*MP+2)/5+1)..", "..math.floor(YOE+ERA*400)+(M<=2 and 1 or 0)
end

据我所知,它可行,但看起来很凌乱,我需要更简洁的实现。 如何以标题所述的标准方式执行此操作?

点赞
用户8621712
用户8621712
print(os.date("%H:%M:%S"))

for k,v in pairs(os.date("*t")) do
    print(k,v)
end

os.date是Lua中的内置函数,第一个参数是格式,可以使用%H:%M:%S*t等参数。前者将返回一个字符串,后者将返回一个包含每个日期类型(秒、月份、日等)的表格。您可以指定第二个参数,否则它将等于os.time()

文档:https://www.lua.org/manual/5.4/manual.html#pdf-os.date

2021-01-31 01:22:52
用户4984564
用户4984564
在所有时区可靠地工作,只要它们与 UTC 相差整数小时。这也假设 Lua 编译时启用了 C99 功能。

os.date("%c", os.time{year=1970, month=1, day=1, hour=math.floor(tonumber(os.date"%z")/60), sec=0})
2021-01-31 09:02:10