months = {} months[9] = { 9, 30, { 3, 21 } } months[10] = { 10, 31, { 8 } } months[11] = { 11, 30, { 2 } } months[12] = { 12, 31, { 7, 24, 25, 26, 27, 28, 31 } } months[13] = { 13, 31, { 1, 2, 3, 4, 25 } } months[14] = { 14, 28, { 18 } } months[15] = { 15, 31, { 1, 11, 12, 13, 14, 15 } } months[16] = { 16, 30, { 19, 22 } } months[17] = { 17, 31, { 20 } } months[18] = { 18, 30, { 7, 28, 29, 30 } } startY = 2018 startM = 9 startD = 4 endY = startY + 1 endM = 18 endD = 27 testY = 2018 testM = 9 testD = 19 currentY = testY currentM = testM currentD = testD function calculateWeekdays() local weekMap = {} for i = startM, 18 do weekMap[i] = {} for j = 1, months[i][2] do if (i == startM and j == 1) then weekMap[startM][1] = 7 else if (j == 1) then weekMap[i][j] = weekMap[i-1][months[i-1][2]] + 1 else weekMap[i][j] = weekMap[i][j-1] + 1 end end if ( weekMap[i][j] > 7 ) then weekMap[i][j] = 1 end end end print("Finished calculating Weekdays. Returning") return weekMap end function calculateSchoolDays() -- DayMap Value 9 == WeekEnd -- DayMap Value 8 == FreeDay local dayMap = {} for i = startM, 18 do dayMap[i] = {} for j = 1, months[i][2] do if (weekDays[i][j] == 1 or weekDays[i][j] == 7) then dayMap[i][j] = 9 -- Sunday (1) or Saturday (7) elseif ( i == startM and j == startD ) then dayMap[i][startD] = 1 -- Start Day elseif ( i == startM and j < startD ) then dayMap[i][j] = 0 -- Before Start Day else --print ("i: "..i.." | j: "..j) --free = false for f = 1, #months[ i ][ 3 ] do if ( j == months[ i ][ 3 ][ f ] ) then dayMap[i][j] = 8 f = #months[ i ][ 3 ] free = true print("Set true | i: "..i.." j: "..j) goto later end end if ( not free ) then if ( j < 4 and i ~= startM ) then -- If the day of the month is less than 4 and the month is not startM (The First month of the school year) if ( j == 1 and dayMap[ i - 1 ][ months[ i - 1 ][ 2 ] ] == 9 ) then dayMap[i][j] = dayMap[ i - 1 ][ months[ i - 1 ][ 2 ] - 2 ] + 1 -- If the last day of the previous month is a weekend (9) then the current day is set to the last day of the previous month - (3 - j)'s day + 1 elseif ( j == 2 and dayMap[ i ][ j - 1] == 9) then dayMap[i][j] = dayMap[i - 1][ months[ i - 1 ][ 2 ] - 1 ] + 1 elseif ( j == 3 and dayMap[ i ][ j - 1] == 9) then dayMap[i][j] = dayMap[i - 1][ months[ i - 1 ][ 2 ] ] + 1 else if ( j == 1) then dayMap[i][j] = dayMap[i - 1][months[i - 1][2]] + 1 -- If j == 1 then the current day number is set to the last day of the previous month's day number + 1 else dayMap[i][j] = dayMap[i][j - 1] + 1 -- If j is NOT 1 then the current day number is set to the previous day number + 1 end end elseif ( dayMap[i][j - 1] == 9 ) then --print(i.." = i | j = "..j) dayMap[i][j] = dayMap[i][j-3] + 1 --dayMap[i][j] = dayMap[i][j-3] + 1 - 10 else --print(i.." = i | j = "..j) dayMap[i][j] = dayMap[i][j - 1] + 1 end else print(">> Free Days <<") print("---------------") print("i: "..i.." j: "..j) found = false daysBack = 1 monthsBack = 0 repeat if ( j <= daysBack ) then daysBack = 0 monthsBack = monthsBack + 1 end if (i == 13 and j == 7) then dayMap[i][j] = 1 print("It is the first school day of January and the New Year, starting on Day 1") found = true elseif ( monthsBack > 0 ) then if ( dayMap[ i - monthsBack ][ months[ i - monthsBack ][ 2 ] - daysBack ] ~= 8 and dayMap[ i - monthsBack ][ months[ i - monthsBack ][ 2 ] - daysBack ] ~= 9 ) then dayMap[ i ][ j ] = dayMap[ i - monthsBack ][ months[ i - monthsBack ][ 2 ] - daysBack ] + 1 print("Months Back: "..monthsBack.." | Days Back: "..daysBack) found = true end else if ( dayMap[ i ][ j - daysBack ] ~= 8 and dayMap[ i ][ j - daysBack ] ~= 9 ) then dayMap[ i ][ j ] = dayMap[ i ][ j - daysBack ] + 1 print("Months Back: "..monthsBack.." | Days Back: "..daysBack) found = true end end daysBack = daysBack + 1 until found free = false end --if (i == 9 and j > 18 and j < 26) then goto later end if ( dayMap[i][j] > 5 ) then dayMap[i][j] = 1 end ::later:: end end end print("Finished calculating School Days. Returning") return dayMap end weekDays = calculateWeekdays() print("Just before Printing Weekdays") --[[for i = startM, 18 do print ("Month: "..i) for j = 1, months[i][2] do if weekDays[i][j] == 1 then WEEKDAY = "Sunday 1" elseif weekDays[i][j] == 2 then WEEKDAY = "Monday" elseif weekDays[i][j] == 3 then WEEKDAY = "Tuesday" elseif weekDays[i][j] == 4 then WEEKDAY = "Wednesday" elseif weekDays[i][j] == 5 then WEEKDAY = "Thursday" elseif weekDays[i][j] == 6 then WEEKDAY = "Friday" elseif weekDays[i][j] == 7 then WEEKDAY = "Saturday 7" else WEEKDAY = "an Unknown Weekday" end print("-- "..j.." is "..WEEKDAY) end end]]-- schoolDays = calculateSchoolDays() print("Just before Printing School Days") for i = startM, 18 do print ("Month: "..i) for j = 1, months[i][2] do if weekDays[i][j] == 1 then WEEKDAY = "Sunday 1" elseif weekDays[i][j] == 2 then WEEKDAY = "Monday" elseif weekDays[i][j] == 3 then WEEKDAY = "Tuesday" elseif weekDays[i][j] == 4 then WEEKDAY = "Wednesday" elseif weekDays[i][j] == 5 then WEEKDAY = "Thursday" elseif weekDays[i][j] == 6 then WEEKDAY = "Friday" elseif weekDays[i][j] == 7 then WEEKDAY = "Saturday 7" else WEEKDAY = "an Unknown Weekday" end if schoolDays[i][j] == 0 then SCHOOLDAY = "Before School Started" elseif schoolDays[i][j] == 1 then SCHOOLDAY = "Day 1" elseif schoolDays[i][j] == 2 then SCHOOLDAY = "Day 2" elseif schoolDays[i][j] == 3 then SCHOOLDAY = "Day 3" elseif schoolDays[i][j] == 4 then SCHOOLDAY = "Day 4" elseif schoolDays[i][j] == 5 then SCHOOLDAY = "Day 5" elseif schoolDays[i][j] == 8 then SCHOOLDAY = "a Free Day" elseif schoolDays[i][j] == 9 then SCHOOLDAY = "a Weekend Day" else SCHOOLDAY = "an Unknown Day ("..schoolDays[i][j]..")" end print("-- "..j.." is "..WEEKDAY.." and is "..SCHOOLDAY) end end function getSchoolDay(year, month, day) if (year > startY) then month = month + 12 end end
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more