diff --git a/lua/sca_main.lua b/lua/sca_main.lua index baf2941..13ccd13 100644 --- a/lua/sca_main.lua +++ b/lua/sca_main.lua @@ -3,23 +3,16 @@ local isRunning = false local summerTime = nil local function sca_setup_timezone() - local time = os.time() // Year in seconds - local currentYear = 1970 + (math.floor(time / 31536000)) - local leapYears = 0 - local isLeapYear = tobool((currentYear % 4 == 0) and (currentYear % 100 != 0 and currentYear % 400 == 0)) + local time = os.time() + local daysPastUnixYear = math.floor(time / 86400) // Unix began on 01.01.1970 + local year = 1970 + local month = 0 + local day = 0 + local weekDay = daysPastUnixYear % 7 // 0 = thursday, 1 = friday, 2 = saturday, 3 = sunday, 4 = monday, 5 = tuesday, 6 = wensday. 01.01.1970 was on thursday - for (i = 1970, currentYear, 1) do - if ((i % 4 == 0) and (i % 100 != 0 and i % 400 == 0)) then - leapYears = leapYears + 1 - end - end - // Year in seconds // Day in seconds - local yearBeginTimestamp = (31536000 * (currentYear - 1970)) + (86400 * leapYears) - local daysPastYearBegin = math.floor((yearBeginTimestamp - time) / 86400) - - local daysPerMonth = - { + local daysPerMonth = { [0] = 31, // January + [1] = 28, // February [2] = 31, // March [3] = 30, // April [4] = 31, // May @@ -27,19 +20,41 @@ local function sca_setup_timezone() [6] = 31, // July [7] = 31, // August [8] = 30, // September - [9] = 31, // October - [10] = 30, // November + [9] = 31, // Ocotober + [10] = 30, // Novmeber [11] = 31 // December } - local daysInTotal = 0 - for (i = 0, 11, 1) do - daysInTotal = daysInTotal + daysPerMonth[i] - if (daysPastYearBegin / daysInTotal > 1) then - continue + while (daysPastUnixYear > 364) do + if (year % 4 == 0 && (year % 400 == 0 && year % 100 != 0)) then + daysPastUnixYear = daysPastUnixYear - 366 else - local currentDay = (yearBeginTimestamp + (daysInTotal * 86400)) - yearBeginTimestamp + daysPastUnixYear = daysPastUnixYear - 365 end + year = year + 1 + end + + for (month, 11, 1) do + if (daysPastUnixYear - daysPerMonth[i] >= daysPerMonth[i]) then + if (i == 2 && (year % 4 == 0 && (year % 400 == 0 && year % 100 != 0))) then + daysPastUnixYear = daysPastUnixYear - (daysPerMonth[i] + 1) + else + daysPastUnixYear = daysPastUnixYear - daysPerMonth[i] + end + else + day = daysPastUnixYear - 1 + break + end + end + + local hourInSeconds = time % 86400 + + if (month >= 2 && month <= 9) then + // Summer time + + else + // Winter time + end end