0

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
Kellen
  • 1
  • 1
  • If you want it to be yearly recurring, you could leave out the year as a property and instead use the current year. Then change the if clause for dif <=0 to increment the year. – usysrc Sep 21 '21 at 10:21
  • @usysrc [here is the fixed code](https://pastebin.com/uV5tJiek) -- I solved this issue, but now this broke the RELEASETEXT section... it basically resets the years, skips over them to restart, but when the day inputted in the target date reaches 0, it shows nothing. No text. – Kellen Sep 21 '21 at 18:16

1 Answers1

0

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
usysrc
  • 116
  • 6