How can I make this Rainmeter skin restart the same countdown each and every year?

377 Views Asked by At

I've been working on editing/recreating a Rainmeter skin (sorry if these codes are a bit messy) and I can't figure out how to restart the countdown (ie. Christmas) every year. I basically want this countdown clock to loop, if that makes sense... I've attached the .ini code and the .lua code.

[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

And here is the .lua code


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
1

There are 1 best solutions below

0
On

You could recalculate if you are a day over:

-- 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