small rewrite

This commit is contained in:
2024-03-26 22:11:01 +01:00
parent 5d433a0949
commit 92b051e5a2

View File

@@ -3,23 +3,16 @@ local isRunning = false
local summerTime = nil local summerTime = nil
local function sca_setup_timezone() local function sca_setup_timezone()
local time = os.time() // Year in seconds local time = os.time()
local currentYear = 1970 + (math.floor(time / 31536000)) local daysPastUnixYear = math.floor(time / 86400) // Unix began on 01.01.1970
local leapYears = 0 local year = 1970
local isLeapYear = tobool((currentYear % 4 == 0) and (currentYear % 100 != 0 and currentYear % 400 == 0)) 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 local daysPerMonth = {
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 =
{
[0] = 31, // January [0] = 31, // January
[1] = 28, // February
[2] = 31, // March [2] = 31, // March
[3] = 30, // April [3] = 30, // April
[4] = 31, // May [4] = 31, // May
@@ -27,19 +20,41 @@ local function sca_setup_timezone()
[6] = 31, // July [6] = 31, // July
[7] = 31, // August [7] = 31, // August
[8] = 30, // September [8] = 30, // September
[9] = 31, // October [9] = 31, // Ocotober
[10] = 30, // November [10] = 30, // Novmeber
[11] = 31 // December [11] = 31 // December
} }
local daysInTotal = 0 while (daysPastUnixYear > 364) do
for (i = 0, 11, 1) do if (year % 4 == 0 && (year % 400 == 0 && year % 100 != 0)) then
daysInTotal = daysInTotal + daysPerMonth[i] daysPastUnixYear = daysPastUnixYear - 366
if (daysPastYearBegin / daysInTotal > 1) then
continue
else else
local currentDay = (yearBeginTimestamp + (daysInTotal * 86400)) - yearBeginTimestamp daysPastUnixYear = daysPastUnixYear - 365
end 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
end end