将Unix时间转换为易读的日期和时间

我正在使用 Lua 开发,但不支持日期,那么有哪些方法可以将 Unix 毫秒时间转换为日期和时间 mm/dd/yyyy hh:mm:ss?

我有时间和天,但年份和月份很难。

谢谢。

编辑:现在我只需要一个月的天数,我已经想出了其他部分。

编辑:代码的第一部分没有一个月的天数,仍在处理 DST。


TIMEZONE=-5
DAYLIGHTSAVING=0--未完成

while true do
 t=tick()
 y=math.floor(1970+ t /31556926)
 ds=((1970+t/31556926)-y)*31556926
 m=math.floor(ds/2629743)+1
 d=math.floor(ds/86400)+1
 md=0--未完成
 wd=d%7+6
 print(md,wd)
 h=math.floor(math.fmod(t,60*60*24)/3600) + 5 + (TIMEZONE) + (DAYLIGHTSAVING)
 mn= math.floor(math.fmod(t,60*60*24)/60 - 60*h)
 s= math.floor(math.fmod(math.fmod(t,60*60*24),60))
 print(m,d,y)
 print(h,mn,s)
 wait(1)
end

在获得每月的天数和 DST 后,我会使其更美观。

原文链接 https://stackoverflow.com/questions/4460902

点赞
stackoverflow用户323682
stackoverflow用户323682

对于那些想知道如何做到的人...

这是我是如何做到的:


时区=-5
夏令时=0

dm={31,28,31,30,31,30,31,31,30,31,30,31}

while true do
t=tick()
y=math.floor(1970+ t /31556926)
ds=((1970+t/31556926)-y)*31556926
m=math.floor(ds/2629743)+1
d=math.floor(ds/86400)+1
md=math.floor(((ds/2629743+1)-m)*dm[m])+1
wd=d%7+6
if(m==11)then 夏令时=0 else 夏令时=1 end
if(m==3)then if(md>=14)then 夏令时=1 else 夏令时=0 end end
if(m==11)then if(md>=7)then 夏令时=0 else 夏令时=1 end end
h=math.floor(math.fmod(t,60*60*24)/3600) + 5 + (时区) + (夏令时)
mn= math.floor(math.fmod(t,60*60*24)/60 - 60*(h-夏令时))
s= math.floor(math.fmod(math.fmod(t,60*60*24),60))
wait(1)
end
2010-12-16 14:06:16