Simple Math and Implementation Help

I have been working on my very first project in defold, but i have seem to hit a brick wall.
A little guidance would me much appreciated.

Given:


-- --------------------------------------------------------------------------------------------------------------- --
-- 													NEEDED VARIABLES											   --
-- --------------------------------------------------------------------------------------------------------------- --_G

-- collects OS date for use in other gui
local DATEYEAR = os.date("%Y")
local DATEMONTH = tonumber(os.date("%m"))
local WEEKCODE = tonumber(os.date("%w"))

-- sets each day of the week to a corresponding numerical value
local MONDAY = 0
local TUESDAY = 1
local WEDNESDAY = 2
local THURSDAY = 3
local FRIDAY = 4
local SATURDAY = 5
local SUNDAY = 6

-- sets a table for the last day of each month
local LASTDAY = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

-- sets the start day of the calender as JAN, 1 2025 on WEDNESDAY the last day as JAN 31, 2025 on FRIDAY
local STARTDAY = {01, 01, 2025, WEDNESDAY, LASTDAY[1], FRIDAY}

-- needed variables to actually cycle through the weeks
local STARTDAYDIFF = 6 - STARTDAY[4]
local LASTDAYDIFF = 6 - STARTDAY[6]

-- ---------------------------------------------------------------------------------------------------------- --
-- 												NEEDED FUNCTIONS											  --
-- ---------------------------------------------------------------------------------------------------------- --_G


-- function used to clear all text from gui elements
local function refreshcalender()
	for i = 1, 35 do
		gui.set_text(gui.get_node("text_day" .. i), "")
	end
end

-- function used to propagate gui elements
local function startcalender()
	for i = STARTDAY[2], STARTDAY[5] do
		gui.set_text(gui.get_node("text_day" .. i + STARTDAY[4]), i)
	end
end

-- currently unsure if needed. was needed in previous itteration
local function whatistoday()
	msg.post("ui_month#ui_month", DATEMONTH)
	msg.post("ui_year#ui_year", DATEYEAR)
end

function init(self)
	whatistoday()
	msg.post(".", "acquire_input_focus")

	-- calls function to propagate calender on initialization
	startcalender()
	
end

function final(self)
	
end

function update(self, dt)
	
end

function on_message(self, message_id, message, sender)

	-- when receiving message from ui_month#ui_month "UP Year"
	if message_id == hash("UP Month") then
		-- alters the STARTDAY to the first of the next month
		STARTDAY  = { STARTDAY[1] + 1, 1, 2025, STARTDAY[6] + 1, LASTDAY[1 + 1], STARTDAY[4] + STARTDAY[5] / 7}  --??
		--  rolls over to the next week
		if STARTDAY[4] > 6 then
			STARTDAY[4] = LASTDAYDIFF
		end
		refreshcalender()
		startcalender()
	elseif message_id == hash("DOWN Month") then
		startcalender()
	end
	if message_id == hash("year") then
		message[1] = YEAR
		
		
	end
end

function on_input(self, action_id, action)
	print(WEEKCODE)
end

function on_reload(self)
	
end

I am trying to figure out how to calculate the Day of the week the next month should start on.

if message_id == hash("UP Month") then
		-- alters the STARTDAY to the first of the next month
		STARTDAY  = { STARTDAY[1] + 1, 1, 2025, STARTDAY[6] + 1, LASTDAY[1 + 1], STARTDAY[4] + STARTDAY[5] / 7}  --??
		--  rolls over to the next week
		if STARTDAY[4] > 6 then
			STARTDAY[4] = LASTDAYDIFF
		end
		refreshcalender()
		startcalender()
	elseif message_id == hash("DOWN Month") then
		startcalender()
	end
	if message_id == hash("year") then
		message[1] = YEAR
		
		
	end
end

Currently trying to do this with the above, and it works for the first 3 months, but breaks down after that.

Any advise?

ps: I know my code is just ugly af, but this is my first real project and I am trying no to do the whole copy/paste thing.

Thank you.

1 Like

There are built in functionality in Lua to work with time and dates:

-- get the date now as a Lua table
local now = os.date("*t")
-- create a new table with values representing
-- the first day of the next month
local next_month = {
	month = now.month,
	day = 1,
	year = now.year
}
if next_month.month == 12 then
	next_month.month = 1
	next_month.year = now.year + 1
else
	next_month.month = next_month.month + 1
end
-- get the timestamp of the first day of the next month
local time_next_month = os.time(next_month)

-- convert the timestamp of the next month to a table
local date_next_month = os.date("*t", time_next_month)
pprint(date_next_month)

DEBUG:SCRIPT: 
{ --[[0x13e847a60]]
  month = 3,
  day = 1,
  year = 2025,
  min = 0,
  sec = 0,
  hour = 12,
  wday = 7,
  yday = 60,
  isdst = false
}

You want wday which is the day of the week. Sunday is 1, Monday 2 and so on until Saturday which is 7

2 Likes