• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[LUA] get Day of next week

hodleo

Formerly cbrm -Crypto enthusiast, Retired scripter
Staff member
Global Moderator
Joined
Jan 6, 2009
Messages
6,598
Solutions
3
Reaction score
955
Location
Caribbean Sea
tfs 0.3.5 / 0.3.6
I need a globalevent that doesn't repeat twice per week, therefore I'd need a function that returns day of the next week as monthday number

I've googled this a bit but it appears in python, msexcel, etc

Lua:
function getYearDay(dd, mm, yy)
    return os.date('*t',os.time{year = yy, month = mm, day = dd})['yday']
end

the main function I haven't managed to finish, in this case the sunday of next week since current time, in yearday format
Lua:
function getNextSunday()
        local value = 0
    for day = os.date('%j')+1, os.date('%j')+7 do
        if day <= getYearDay(31, 12, 2012) then
            if os.date('%A') == 'Sunday' then --smth like if os.date(iterator's day, iterator's month) -> 'Sunday'
                return --to return the next Sunday's monthday #
            end
        end
    end
end

there's that thing of leap year too


--so far I got this, but it could be reduced I guess
Lua:
function getMonthDays(mm, yy)
    return os.date('*t',os.time{year = yy, month = mm+1, day = 0})['day']
end

function getNextDay(name)
    local mm, yy = os.date('%m'), os.date('%Y')
    for day = os.date('%d')+1, os.date('%d')+7 do
        if day > getMonthDays(mm, yy) then
            mm = mm+1
            day = day-getMonthDays(mm, yy)
        end
        if os.date('%A',os.time{year = yy, month = mm, day = day}) == name then
            return day
        end
    end
end

print(getNextDay('Sunday'))
 
Last edited:
Is this globalevent executing in randomly days? This week in Sunday in next week in Friday and then in next week in Monday?
 
always a random day, but for ex if execution day is Monday, the randomizer can pick another day of same week(Tuesday, etc)
I'm changing the script's globalstorage dayname string to yearday or weekday number for this
 
It can't be something like that?

Lua:
function ...
	if os.date('%A', getGlobalStorage(storage)) == os.date('%A', os.time(t)) then -- check that today is day to execute
		-- execute script
		doSetGlobalStorage(storage, os.time(t) + math.random(1,7)*3600*24) -- random day +1 - +7
	end
end
 
Last edited:
this will do it for me, but it's inefficient
Lua:
function getMonthDays(mm, yy)
	return os.date('*t',os.time{year = yy, month = mm+1, day = 0})['day']
end


function getDayofNextWeek(name)
    local dd, mm, yy = os.date('%d'), os.date('%m'), os.date('%Y')
    for i = 1, 9 do
        day = dd+i
        if day > getMonthDays(mm, yy) then
            mm = os.date('%m')+1
            if mm > 12 then
                mm = 1
                yy = os.date('%Y')+1
            end
            day = math.abs(getMonthDays(mm, yy)-dd-i+1)
        end
        if day > 0 and os.date('%A',os.time{year = yy, month = mm, day = day}) == name then
            return day
        end
    end
end

print(getDayofNextWeek('Sunday'))
even if next desired day is in another month or year
 
Last edited:
What about this?

Lua:
function getDayofNextWeek(name)
    for i = 1, 9 do
        if os.date('%A', os.time(t)+24*3600*i) == name then
            return os.date('%d', os.time(t)+24*3600*i)
        end
    end
end
 
print(getDayofNextWeek('Friday'))
 
Back
Top