Time 如何将历元后的秒数转换为当前日期和时间?

Time 如何将历元后的秒数转换为当前日期和时间?,time,lua,epoch,Time,Lua,Epoch,我知道我不久前发布了这个,但我找到了解决办法。我为一款名为Roblox的游戏编写了这段代码,但我只是在这里发布这段代码,以防其他有同样问题的人需要解决。不管怎样,代码如下: outputTime = true -- true: will print the current time to output window. false: won't print time createVariable = true -- true: creates variables under game.Lighti

我知道我不久前发布了这个,但我找到了解决办法。我为一款名为Roblox的游戏编写了这段代码,但我只是在这里发布这段代码,以防其他有同样问题的人需要解决。不管怎样,代码如下:

outputTime = true -- true: will print the current time to output window. false: won't print time
createVariable = true -- true: creates variables under game.Lighting. false: won't create variables

-----------------------------------------------------------------------------------------------
--DO NOT EDIT BELOW----------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------

if(createVariable) then
    yearVar = Instance.new("IntValue", game.Lighting)
    yearVar.Name = "Year"
    yearVar.Value = 0
    monthVar = Instance.new("IntValue", game.Lighting)
    monthVar.Name = "Month"
    monthVar.Value = 0
    dayVar = Instance.new("IntValue", game.Lighting)
    dayVar.Name = "Day"
    dayVar.Value = 0
    hourVar = Instance.new("IntValue", game.Lighting)
    hourVar.Name = "Hour"
    hourVar.Value = 0
    minuteVar = Instance.new("IntValue", game.Lighting)
    minuteVar.Name = "Minute"
    minuteVar.Value = 0
    secondVar = Instance.new("IntValue", game.Lighting)
    secondVar.Name = "Second"
    secondVar.Value = 0
    dayOfWeek = Instance.new("StringValue", game.Lighting)
    dayOfWeek.Name = "DayOfWeek"
    dayOfWeek.Value = "Thursday"
end
function giveZero(data)
    if string.len(data) <= 1 then
        return "0" .. data
    else
        return data
    end
end
function hasDecimal(value)
    if not(value == math.floor(value)) then
        return true
    else
        return false
    end
end
function isLeapYear(year)
    if(not hasDecimal(year / 4)) then
        if(hasDecimal(year / 100)) then
            return true
        else
            if(not hasDecimal(year / 400)) then
                return true
            else
                return false
            end
        end
    else
        return false
    end
end
local eYear = 1970
local timeStampDayOfWeak = 5
local secondsInHour = 3600
local secondsInDay = 86400
local secondsInYear = 31536000
local secondsInLeapYear = 31622400
local monthWith28 = 2419200
local monthWith29 = 2505600
local monthWith30 = 2592000
local monthWith31 = 2678400
local monthsWith30 = {4, 6, 9, 11}
local monthsWith31 = {1, 3, 5, 7, 8, 10, 12}
local daysSinceEpoch = 0
local DOWAssociates = {"Tursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"}
while(true) do
    now = tick()
    year = 1970
    secs = 0
    daysSinceEpoch = 0
    while((secs + secondsInLeapYear) < now or (secs + secondsInYear) < now) do
        if(isLeapYear(year+1)) then
            if((secs + secondsInLeapYear) < now) then
                secs = secs + secondsInLeapYear
                year = year + 1
                daysSinceEpoch = daysSinceEpoch + 366
            end
        else
            if((secs + secondsInYear) < now) then
                secs = secs + secondsInYear
                year = year + 1
                daysSinceEpoch = daysSinceEpoch + 365
            end
        end
    end
    secondsRemaining = now - secs
    monthSecs = 0
    yearIsLeapYear = isLeapYear(year)
    month = 1 -- January
    while((monthSecs + monthWith28) < secondsRemaining or (monthSecs + monthWith30) < secondsRemaining or (monthSecs + monthWith31) < secondsRemaining) do
        if(month == 1) then
            if((monthSecs + monthWith31) < secondsRemaining) then
                month = 2
                monthSecs = monthSecs + monthWith31
                daysSinceEpoch = daysSinceEpoch + 31
            else
                break
            end
        end
        if(month == 2) then
            if(not yearIsLeapYear) then
                if((monthSecs + monthWith28) < secondsRemaining) then
                    month = 3
                    monthSecs = monthSecs + monthWith28
                    daysSinceEpoch = daysSinceEpoch + 28
                else
                    break
                end
            else
                if((monthSecs + monthWith29) < secondsRemaining) then
                    month = 3
                    monthSecs = monthSecs + monthWith29
                    daysSinceEpoch = daysSinceEpoch + 29
                else
                    break
                end
            end
        end
        if(month == 3) then
            if((monthSecs + monthWith31) < secondsRemaining) then
                month = 4
                monthSecs = monthSecs + monthWith31
                daysSinceEpoch = daysSinceEpoch + 31
            else
                break
            end
        end
        if(month == 4) then
            if((monthSecs + monthWith30) < secondsRemaining) then
                month = 5
                monthSecs = monthSecs + monthWith30
                daysSinceEpoch = daysSinceEpoch + 30
            else
                break           
            end
        end
        if(month == 5) then
            if((monthSecs + monthWith31) < secondsRemaining) then
                month = 6
                monthSecs = monthSecs + monthWith31
                daysSinceEpoch = daysSinceEpoch + 31
            else
                break
            end
        end
        if(month == 6) then
            if((monthSecs + monthWith30) < secondsRemaining) then
                month = 7
                monthSecs = monthSecs + monthWith30
                daysSinceEpoch = daysSinceEpoch + 30
            else
                break
            end
        end
        if(month == 7) then
            if((monthSecs + monthWith31) < secondsRemaining) then
                month = 8
                monthSecs = monthSecs + monthWith31
                daysSinceEpoch = daysSinceEpoch + 31
            else
                break
            end
        end
        if(month == 8) then
            if((monthSecs + monthWith31) < secondsRemaining) then
                month = 9
                monthSecs = monthSecs + monthWith31
                daysSinceEpoch = daysSinceEpoch + 31
            else
                break
            end
        end
        if(month == 9) then
            if((monthSecs + monthWith30) < secondsRemaining) then
                month = 10
                monthSecs = monthSecs + monthWith30
                daysSinceEpoch = daysSinceEpoch + 30
            else
                break
            end
        end
        if(month == 10) then
            if((monthSecs + monthWith31) < secondsRemaining) then
                month = 11
                monthSecs = monthSecs + monthWith31
                daysSinceEpoch = daysSinceEpoch + 31
            else
                break
            end
        end
        if(month == 11) then
            if((monthSecs + monthWith30) < secondsRemaining) then
                month = 12
                monthSecs = monthSecs + monthWith30
                daysSinceEpoch = daysSinceEpoch + 30
            else
                break
            end
        end
    end
    day = 1 -- 1st
    daySecs = 0
    daySecsRemaining = secondsRemaining - monthSecs
    while((daySecs + secondsInDay) < daySecsRemaining) do
        day = day + 1
        daySecs = daySecs + secondsInDay
        daysSinceEpoch = daysSinceEpoch + 1
    end
    hour = 0 -- Midnight
    hourSecs = 0
    hourSecsRemaining = daySecsRemaining - daySecs
    while((hourSecs + secondsInHour) < hourSecsRemaining) do
        hour = hour + 1
        hourSecs = hourSecs + secondsInHour
    end
    minute = 0 -- Midnight
    minuteSecs = 0
    minuteSecsRemaining = hourSecsRemaining - hourSecs
    while((minuteSecs + 60) < minuteSecsRemaining) do
        minute = minute + 1
        minuteSecs = minuteSecs + 60
    end
    second = math.floor(now % 60)
    year = giveZero(year)
    month = giveZero(month)
    day = giveZero(day)
    hour = giveZero(hour)
    minute = giveZero(minute)
    second = giveZero(second)
    remanderForDOW = daysSinceEpoch % 7
    DOW = DOWAssociates[remanderForDOW + 1]
    if(createVariable) then
        yearVar.Value = year
        monthVar.Value = month
        dayVar.Value = day
        hourVar.Value = hour
        minuteVar.Value = minute
        secondVar.Value = second
        dayOfWeek.Value = DOW
    end
    if(outputTime) then
        str = "Year: " .. year .. ", Month: " .. month .. ", Day: " .. day .. ", Hour: " .. hour .. ", Minute: " .. minute .. ", Second: ".. second .. ", Day of Week: " .. DOW
        print(str)
    end
    wait(1)
end
outputTime=true--true:将打印当前输出时间窗口。false:不会打印时间
createVariable=true--true:在game.Lighting下创建变量。false:不会创建变量
-----------------------------------------------------------------------------------------------
--不要在下面编辑----------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
如果是(createVariable),则
yearVar=Instance.new(“IntValue”,game.Lighting)
yearVar.Name=“年”
年变量值=0
monthVar=Instance.new(“IntValue”,game.Lighting)
monthVar.Name=“月”
monthVar.Value=0
dayVar=Instance.new(“IntValue”,game.Lighting)
dayVar.Name=“日”
dayVar.Value=0
hourVar=Instance.new(“IntValue”,game.Lighting)
hourVar.Name=“小时”
小时值=0
minuteVar=Instance.new(“IntValue”,game.Lighting)
minuteVar.Name=“分钟”
分钟变量值=0
secondVar=Instance.new(“IntValue”,game.Lighting)
secondVar.Name=“Second”
secondVar.Value=0
dayOfWeek=Instance.new(“StringValue”,game.Lighting)
dayOfWeek.Name=“dayOfWeek”
dayOfWeek.Value=“星期四”
结束
函数0(数据)

如果string.len(data)这个公式并不简单,有几个原因,特别是闰年。您可能应该在上使用
date
函数,而不是自己尝试计算它。

os.date
是一个标准的Lua函数,如果将第一个参数作为
%t”
传递,它将返回一个包含以下字段的表:
年(四位),
月(1-12),
(1-31),
小时(0-23),
分钟(0-59),
秒(0-61),
wday
(周日为1),
yday
(一年中的一天)和
isdst
(夏时制标志,布尔值)

测试一下:

time = os.time()
print("time since epoch: " .. time)
date = os.date("*t", time)
print("year: " .. date.year)
print("month: " .. date.month)
print("day: " .. date.day)
print("hour: " .. date.hour)
print("minute: " .. date.min)
print("second: " .. date.sec)
print("weekday: " .. date.wday)
输出:

time since epoch: 1374826427
year: 2013
month: 7
day: 26
hour: 16
minute: 13
second: 47
weekday: 6

下面是一些Lua代码,它是从谷歌发现的一些C代码改编而来的。它不处理时区或夏令时,因此输出是指世界协调时间(UTC)

——基于http://www.ethernut.de/api/gmtime_8c_source.html
本地楼层=数学楼层
本地DSEC=24*60*60——一天中的秒数
本地YSEC=365*DSEC——一年中的秒数
本地LSEC=YSEC+DSEC——闰年的秒数
本地FSEC=4*YSEC+DSEC——4年间隔内的秒数
当地基准指数为4——1970-01-01是星期四
当地基准年=1970年——1970年为基准年
本地日={
-1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364
}
本地lpu天数={}
对于i=1,2个do\u lpdays[i]=\u days[i]结束
对于i=3,13个do _lpdays[i]=_days[i]+1个end
函数时间(t)
打印(操作系统日期(“!\n%c\t%j”,t),t)
局部y,j,m,d,w,h,n,s
本地mdays=\u天
s=t
--首先计算四年的间隔数,这样计算
--闰年的定义很简单。顺便说一句,因为2000年是闰年
--2100超出范围,这个公式很简单。
y=楼层(s/FSEC)
s=s-y*FSEC
y=y*4+基准年——1970年、1974年、1978年。。。
如果s>=YSEC,则
y=y+1--1971、1975、1979,。。。
s=s-YSEC
如果s>=YSEC,则
y=y+1--1972年、1976年、1980年……(闰年!)
s=s-YSEC
如果s>=LSEC,则
y=y+1--1971、1975、1979,。。。
s=s-LSEC
否则——闰年
mdays=\u lpdays
结束
结束
结束
j=地板(s/DSEC)
s=s-j*DSEC
局部m=1
而mdays[m]您可以使用


一个更快的解决方案是使用我的方法,因为大多数人都可以访问os.date(),所以我没有看到其他人使用我的方法

由于我没有访问os.date()的权限,以下是我的解决方案:

local tabIndexOverflow = function(seed, table)
    -- This subtracts values from the table from seed until an overflow
    -- This can be used for probability :D
    for i = 1, #table do
        if seed - table[i] <= 0 then
            return i, seed
        end
        seed = seed - table[i]
    end
end

local getDate = function(unix)
    -- Given unix date, return string date
    assert(unix == nil or type(unix) == "number" or unix:find("/Date%((%d+)"), "Please input a valid number to \"getDate\"")
    local unix = (type(unix) == "string" and unix:match("/Date%((%d+)") / 1000 or unix or os.time()) -- This is for a certain JSON compatability. It works the same even if you don't need it

    local dayCount, year, days, month = function(yr) return (yr % 4 == 0 and (yr % 100 ~= 0 or yr % 400 == 0)) and 366 or 365 end, 1970, math.ceil(unix/86400)

    while days >= dayCount(year) do days = days - dayCount(year) year = year + 1 end -- Calculate year and days into that year

    month, days = tabIndexOverflow(days, {31,(dayCount(year) == 366 and 29 or 28),31,30,31,30,31,31,30,31,30,31}) -- Subtract from days to find current month and leftover days

--  hours = hours > 12 and hours - 12 or hours == 0 and 12 or hours -- Change to proper am or pm time
--  local period = hours > 12 and "pm" or "am"

--  Formats for you!
--  string.format("%d/%d/%04d", month, days, year) 
--  string.format("%02d:%02d:%02d %s", hours, minutes, seconds, period)
    return {Month = month, day = days, year = year, hours = math.floor(unix / 3600 % 24), minutes = math.floor(unix / 60 % 60), seconds = math.floor(unix % 60)}
end
local tabIndexOverflow=函数(种子,表)
--这将从seed中减去表中的值,直到溢出
--这可用于概率:D
对于i=1,#表do
如果种子表[i]我就是这样做的

> time0=os.time()
> time0
1571439964
> os.date("%Y%m%d%H%M%S",time0)
20191019120604
>

我不能用日期()函数。我唯一的选择是自己计算它。正如你所知,我使用的是lua的沙盒版本。唯一访问时间的方法是从epoch开始的秒数。我也不能导入或包含任何内容。好的,在这种情况下,试着看看这个问题:注意luatz是一个纯lua库,你可以从
src/timeline复制/粘贴代码。lua
如果需要。
local tabIndexOverflow = function(seed, table)
    -- This subtracts values from the table from seed until an overflow
    -- This can be used for probability :D
    for i = 1, #table do
        if seed - table[i] <= 0 then
            return i, seed
        end
        seed = seed - table[i]
    end
end

local getDate = function(unix)
    -- Given unix date, return string date
    assert(unix == nil or type(unix) == "number" or unix:find("/Date%((%d+)"), "Please input a valid number to \"getDate\"")
    local unix = (type(unix) == "string" and unix:match("/Date%((%d+)") / 1000 or unix or os.time()) -- This is for a certain JSON compatability. It works the same even if you don't need it

    local dayCount, year, days, month = function(yr) return (yr % 4 == 0 and (yr % 100 ~= 0 or yr % 400 == 0)) and 366 or 365 end, 1970, math.ceil(unix/86400)

    while days >= dayCount(year) do days = days - dayCount(year) year = year + 1 end -- Calculate year and days into that year

    month, days = tabIndexOverflow(days, {31,(dayCount(year) == 366 and 29 or 28),31,30,31,30,31,31,30,31,30,31}) -- Subtract from days to find current month and leftover days

--  hours = hours > 12 and hours - 12 or hours == 0 and 12 or hours -- Change to proper am or pm time
--  local period = hours > 12 and "pm" or "am"

--  Formats for you!
--  string.format("%d/%d/%04d", month, days, year) 
--  string.format("%02d:%02d:%02d %s", hours, minutes, seconds, period)
    return {Month = month, day = days, year = year, hours = math.floor(unix / 3600 % 24), minutes = math.floor(unix / 60 % 60), seconds = math.floor(unix % 60)}
end
> time0=os.time()
> time0
1571439964
> os.date("%Y%m%d%H%M%S",time0)
20191019120604
>