4

少し前にこれを投稿したことは知っていますが、解決策を見つけました。私は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

----元の投稿---- エポックからの秒数以外のリソースがない場合、次の計算式は何ですか?

必要なもののリストは次のとおりです。

現在の月 例: 7

現在の日 例: 25

現在の曜日 例: 木曜日 (1 ~ 7 が許容されます)

現在の時刻 例: 22

現在の分 例: 34

現在の秒: 07

4

6 に答える 6

9

これは、Google が見つけた C コードを一部改変した Lua コードです。タイムゾーンや夏時間は処理されないため、出力は協定世界時 (UTC) を参照します。

-- based on http://www.ethernut.de/api/gmtime_8c_source.html

local floor=math.floor

local DSEC=24*60*60 -- secs in a day
local YSEC=365*DSEC -- secs in a year
local LSEC=YSEC+DSEC    -- secs in a leap year
local FSEC=4*YSEC+DSEC  -- secs in a 4-year interval
local BASE_DOW=4    -- 1970-01-01 was a Thursday
local BASE_YEAR=1970    -- 1970 is the base year

local _days={
    -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364
}
local _lpdays={}
for i=1,2  do _lpdays[i]=_days[i]   end
for i=3,13 do _lpdays[i]=_days[i]+1 end

function gmtime(t)
print(os.date("!\n%c\t%j",t),t)
    local y,j,m,d,w,h,n,s
    local mdays=_days
    s=t
    -- First calculate the number of four-year-interval, so calculation
    -- of leap year will be simple. Btw, because 2000 IS a leap year and
    -- 2100 is out of range, this formula is so simple.
    y=floor(s/FSEC)
    s=s-y*FSEC
    y=y*4+BASE_YEAR         -- 1970, 1974, 1978, ...
    if s>=YSEC then
        y=y+1           -- 1971, 1975, 1979,...
        s=s-YSEC
        if s>=YSEC then
            y=y+1       -- 1972, 1976, 1980,... (leap years!)
            s=s-YSEC
            if s>=LSEC then
                y=y+1   -- 1971, 1975, 1979,...
                s=s-LSEC
            else        -- leap year
                mdays=_lpdays
            end
        end
    end
    j=floor(s/DSEC)
    s=s-j*DSEC
    local m=1
    while mdays[m]<j do m=m+1 end
    m=m-1
    local d=j-mdays[m]
    -- Calculate day of week. Sunday is 0
    w=(floor(t/DSEC)+BASE_DOW)%7
    -- Calculate the time of day from the remaining seconds
    h=floor(s/3600)
    s=s-h*3600
    n=floor(s/60)
    s=s-n*60
    print("y","j","m","d","w","h","n","s")
    print(y,j+1,m,d,w,h,n,s)
end

local t=os.time()
gmtime(t)

t=os.time{year=1970, month=1, day=1, hour=0} gmtime(t)
t=os.time{year=1970, month=1, day=3, hour=0} gmtime(t)
t=os.time{year=1970, month=1, day=2, hour=23-3, min=59, sec=59} gmtime(t)
于 2013-07-26T19:32:22.927 に答える
3

この式は、いくつかの理由、特にうるう年のために単純ではありません。自分で計算するのではなく、このページdateの関数を使用する必要があります。

于 2013-07-26T04:41:27.037 に答える
2

ルアッツを使用できます

x = 1234567890
t = require "luatz.timetable".new_from_timestamp ( x )

print(t.year,t.month,t.day,t.hour,t.min,t.sec,t.yday,t.wday)
-- Or just
print(t:rfc_3339())
于 2013-08-06T00:36:49.067 に答える
1

はるかに高速な解決策は、ほとんどの人が 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

ただし、曜日を見つけることに関しては、自分で行う必要があります。それがトールの日なのか、それともフリージの日なのか、私は知りたくありませんでした。

于 2015-12-18T09:15:52.053 に答える
0

os.date標準の Lua 関数です。最初の引数を として渡すと"%t"、次のフィールドを含むテーブルが返されます: year(4 桁)、month(1--12)、day(1--31)、hour(0--23)、min( 0--59)、sec(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
于 2013-07-26T08:14:19.630 に答える