0

我一直在编辑/重新创建 Rainmeter 皮肤(抱歉,如果这些代码有点乱),我不知道如何每年重新开始倒计时(即圣诞节)。我基本上希望这个倒计时时钟循环,如果这有意义的话......我已经附上了.ini代码和.lua代码。

[Rainmeter]
Update=1000

[Variables]
Color=0,0,0
FontName=Franklin Gothic Heavy
FontHeight=46

;Set these variables so change the day the count down is counting to, and to change what the text says when it hits that time!;

toYear=2021
toMonth=12
toDay=25
toHour=24
toMinute=0
toSecond=0
ReleaseText="0"

;Measures;

[MeasureScript]
Measure=script
ScriptFile=#CURRENTPATH#countdown.lua
TableName=Countdown
year=#toYear#
month=#toMonth#
day=#toDay#
hour=#toHour#
min=#toMinute#
sec=#toSecond#
fintext=#ReleaseText#

;Meters;

[MeterLogo]
Meter=Image
ImageName=Xmas.png
SolidColor=0,0,0,1
X=R
Y=0

[MeterString]
Meter=string
MeasureName=MeasureScript
X=555
Y=38
H=1
FontFace=#FontName#
FontSize=#FontHeight#
FontColor=#Color#
StringStyle=BOLD
Text=%1
AntiAlias=1
StringAlign=Center
Group=Static

[MeterTextTop]

[MeterTextMiddle]
Meter=String
Text=Insert text
X=308
Y=131
FontColor=0,0,0
StringStyle=BOLD
FontSize=20
FontFace=Franklin Gothic Heavy
AntiAlias=1
StringAlign=Center

[MeterTextBottom]
Meter=String
Text= Hello!
X=320
Y=190
FontColor=255,255,255
StringStyle=BOLD
FontSize=20
FontFace=Franklin Gothic Heavy
AntiAlias=1
StringAlign=Center

这是 .lua 代码


PROPERTIES = {year=0, month=0, day=0, hour=0, min=0, sec=0, fintext=""}

function Initialize()

    RELEASEDATE = {}
    setmetatable(RELEASEDATE, getmetatable(PROPERTIES))
    for k,v in pairs(PROPERTIES) do
        if k ~= fintext then
            RELEASEDATE[k] = v
        end
    end
    RELEASEDATE.isdst = true

    RELEASETEXT = PROPERTIES.fintext or ""

end

function GetTimeLeft()
    local dif = os.time(RELEASEDATE) - os.time()
    local timeleft = {
        [1] = math.floor(dif/60/60/24), --day
    }

    local text = {}
    for i=1, #timeleft do
        if i == 1 then
            if timeleft[i] > 0 then
                table.insert(text,timeleft[i])
            end
        else
            table.insert(text,timeleft[i])
        end
    end

    if dif <= 0 then
        text = RELEASETEXT
    else
        text = table.concat(text,":")
    end

    return tostring(text)
end

function Update()
end

function GetStringValue()

    return GetTimeLeft()

end

local function centerText(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.round((x / 2) - (text:len() / 2)), y2)
write(text)
end
4

1 回答 1

0

如果您超过一天,您可以重新计算:

-- this is just an example for testing
PROPERTIES = {year=2021, month=9, day=23, fintext="Its christmas!"}

function Initialize()

    RELEASEDATE = {}
    setmetatable(RELEASEDATE, getmetatable(PROPERTIES))
    for k,v in pairs(PROPERTIES) do
        if k ~= fintext then
            RELEASEDATE[k] = v
        end
    end
    RELEASEDATE.isdst = true
    RELEASEDATE.year = os.date("%Y")

    RELEASETEXT = PROPERTIES.fintext or ""
end

function GetTimeLeft()
    local dif = os.time(RELEASEDATE) - os.time{year=os.date("%Y"),month=os.date("%m"), day=os.date("%d")}
    if dif < 0 then
        RELEASEDATE.year = os.date("%Y")+1
        return GetTimeLeft()
    end
    
    local timeleft = {
        [1] = math.floor(dif/60/60/24), --day
    }

    local text = {}
    for i=1, #timeleft do
        if i == 1 then
            if timeleft[i] > 0 then
                table.insert(text,timeleft[i])
            end
        else
            table.insert(text,timeleft[i])
        end
    end

    if dif <= 0 then
        text = RELEASETEXT
    else
        text = table.concat(text,":")
    end
    return tostring(text)
end

-- this is just for testing
Initialize()
for i=1, 5 do
    print(GetTimeLeft() .. " day left till christmas")
end
于 2021-09-21T22:47:41.657 回答