Refactoring the summer time function needs an struct as parameter instead of each time variable on its own
This commit is contained in:
parent
7df7c53159
commit
591f0f496a
29
timecore.lua
29
timecore.lua
@ -17,30 +17,31 @@
|
||||
--}
|
||||
|
||||
-- @fn isSummerTime(year, month, day, hour, minute, second,dow)
|
||||
-- @param year Current year of day range (2016 - ...)
|
||||
-- @param month Current month of year range (1 - 12) e.g. 2 is Februrary
|
||||
-- @param day Current day in month range (1 - 31)
|
||||
-- @param hour Current hour of day range (0 - 23)
|
||||
-- @param minute Current minute in hour range (0 - 59)
|
||||
-- @param second Current second of minute range (0 - 59)
|
||||
-- @param dow Current day of week range (1 - 7) (1 is Monday, 7 is Sunday)
|
||||
-- The @param time is a struct containing the following entries:
|
||||
-- @var year Current year of day range (2016 - ...)
|
||||
-- @var month Current month of year range (1 - 12) e.g. 2 is Februrary
|
||||
-- @var day Current day in month range (1 - 31)
|
||||
-- @var hour Current hour of day range (0 - 23)
|
||||
-- @var minute Current minute in hour range (0 - 59)
|
||||
-- @var second Current second of minute range (0 - 59)
|
||||
-- @var dow Current day of week range (1 - 7) (1 is Monday, 7 is Sunday)
|
||||
-- @return <code>true</code> if we have currently summer time
|
||||
-- @return <code>false</code> if we have winter time
|
||||
function isSummerTime(year, month, day, hour, minute, second,dow)
|
||||
function isSummerTime(time)
|
||||
-- we are in 100% in the summer time
|
||||
if (month > 3 and month < 10) then
|
||||
if (time.month > 3 and time.month < 10) then
|
||||
return true
|
||||
-- March is not 100% Summer time, only starting at the last sunday
|
||||
elseif ((month == 3 and day >= 25 and day <= 31 and hour > 2 and dow == 7) or
|
||||
elseif ((time.month == 3 and time.day >= 25 and time.day <= 31 and time.hour > 2 and time.dow == 7) or
|
||||
-- Only handle days after the last sunday in this month
|
||||
((month == 3 and day >= 25 and day <= 31 and dow < 7 and ((7-dow + day) > 31))) ) then
|
||||
((time.month == 3 and time.day >= 25 and time.day <= 31 and time.dow < 7 and ((7-time.dow + time.day) > 31))) ) then
|
||||
-- summer time
|
||||
return true
|
||||
-- October is not 100% Summer time, ending with the last sunday
|
||||
elseif ((month == 10 and day >= 25 and day <= 31 and (hour < 2 or (hour == 2 and minute== 0 and second == 0)) and dow == 7) or
|
||||
(month == 10 and day >= 25 and day <= 31 and dow < 7 and ((7-dow + day) <= 31)) or
|
||||
elseif ((time.month == 10 and time.day >= 25 and time.day <= 31 and (time.hour < 2 or (time.hour == 2 and time.minute== 0 and time.second == 0)) and time.dow == 7) or
|
||||
(time.month == 10 and time.day >= 25 and time.day <= 31 and time.dow < 7 and ((7-time.dow + time.day) <= 31)) or
|
||||
-- Handle all days up to the 25. of october
|
||||
(month == 10 and day < 25 )
|
||||
(time.month == 10 and time.day < 25 )
|
||||
)then
|
||||
-- summer time
|
||||
return true
|
||||
|
@ -9,7 +9,17 @@ WINTERTIME_OFFSET=1
|
||||
-- so resultingHourDiff is 1 in wintertime
|
||||
-- and 2 in summertime
|
||||
function checkTime(yearIn, monthIn, dayIn, hourIn, minutesIn, secondsIn,dowIn, resultingHourDiff)
|
||||
local summerTime = isSummerTime(yearIn, monthIn, dayIn, hourIn, minutesIn, secondsIn, dowIn)
|
||||
-- Generate a time struct from the given parameter
|
||||
time = {}
|
||||
time.year = yearIn
|
||||
time.month = monthIn
|
||||
time.day = dayIn
|
||||
time.hour = hourIn
|
||||
time.minute = minutesIn
|
||||
time.second = secondsIn
|
||||
time.dow = dowIn
|
||||
-- the test itself
|
||||
local summerTime = isSummerTime(time)
|
||||
if ((resultingHourDiff == SUMMERTIME_OFFSET and not (summerTime == true)) or (resultingHourDiff == WINTERTIME_OFFSET and not (summerTime == false))) then
|
||||
print(yearIn .. "-" .. string.format("%0.2d", monthIn) .. "-" .. string.format("%0.2d", dayIn) .. " " .. string.format("%0.2d", hourIn) .. ":" .. string.format("%0.2d", minutesIn) .. ":" .. string.format("%0.2d", secondsIn) .. " (dow:" .. dowIn .. ") was not GMT+" .. resultingHourDiff .. " ( summer time was " .. tostring(summerTime) .. ")" )
|
||||
os.exit(1)
|
||||
|
@ -9,7 +9,17 @@ WINTERTIME_OFFSET=1
|
||||
-- so resultingHourDiff is 1 in wintertime
|
||||
-- and 2 in summertime
|
||||
function checkTime(yearIn, monthIn, dayIn, hourIn, minutesIn, secondsIn,dowIn, resultingHourDiff)
|
||||
local summerTime = isSummerTime(yearIn, monthIn, dayIn, hourIn, minutesIn, secondsIn, dowIn)
|
||||
-- Generate a time struct from the given parameter
|
||||
time = {}
|
||||
time.year = yearIn
|
||||
time.month = monthIn
|
||||
time.day = dayIn
|
||||
time.hour = hourIn
|
||||
time.minute = minutesIn
|
||||
time.second = secondsIn
|
||||
time.dow = dowIn
|
||||
-- the test itself
|
||||
local summerTime = isSummerTime(time)
|
||||
if ((resultingHourDiff == SUMMERTIME_OFFSET and not (summerTime == true)) or (resultingHourDiff == WINTERTIME_OFFSET and not (summerTime == false))) then
|
||||
print(yearIn .. "-" .. string.format("%0.2d", monthIn) .. "-" .. string.format("%0.2d", dayIn) .. " " .. string.format("%0.2d", hourIn) .. ":" .. string.format("%0.2d", minutesIn) .. ":" .. string.format("%0.2d", secondsIn) .. " (dow:" .. dowIn .. ") was not GMT+" .. resultingHourDiff .. " ( summer time was " .. tostring(summerTime) .. ")" )
|
||||
os.exit(1)
|
||||
@ -17,6 +27,7 @@ function checkTime(yearIn, monthIn, dayIn, hourIn, minutesIn, secondsIn,dowIn, r
|
||||
print(yearIn .. "-" .. string.format("%0.2d", monthIn) .. "-" .. string.format("%0.2d", dayIn) .. " " .. string.format("%0.2d", hourIn) .. ":" .. string.format("%0.2d", minutesIn) .. ":" .. string.format("%0.2d", secondsIn) .. " summertime is " .. tostring(summerTime) .. " (GMT+" .. resultingHourDiff .. ")" )
|
||||
end
|
||||
end
|
||||
|
||||
checkTime(2016, 3, 1, 0, 14, 34, 2, 1)
|
||||
checkTime(2016, 3, 1, 1, 53, 5, 2, 1)
|
||||
checkTime(2016, 3, 1, 2, 19, 51, 2, 1)
|
||||
|
@ -9,7 +9,17 @@ WINTERTIME_OFFSET=1
|
||||
-- so resultingHourDiff is 1 in wintertime
|
||||
-- and 2 in summertime
|
||||
function checkTime(yearIn, monthIn, dayIn, hourIn, minutesIn, secondsIn,dowIn, resultingHourDiff)
|
||||
local summerTime = isSummerTime(yearIn, monthIn, dayIn, hourIn, minutesIn, secondsIn, dowIn)
|
||||
-- Generate a time struct from the given parameter
|
||||
time = {}
|
||||
time.year = yearIn
|
||||
time.month = monthIn
|
||||
time.day = dayIn
|
||||
time.hour = hourIn
|
||||
time.minute = minutesIn
|
||||
time.second = secondsIn
|
||||
time.dow = dowIn
|
||||
-- the test itself
|
||||
local summerTime = isSummerTime(time)
|
||||
if ((resultingHourDiff == SUMMERTIME_OFFSET and not (summerTime == true)) or (resultingHourDiff == WINTERTIME_OFFSET and not (summerTime == false))) then
|
||||
print(yearIn .. "-" .. string.format("%0.2d", monthIn) .. "-" .. string.format("%0.2d", dayIn) .. " " .. string.format("%0.2d", hourIn) .. ":" .. string.format("%0.2d", minutesIn) .. ":" .. string.format("%0.2d", secondsIn) .. " (dow:" .. dowIn .. ") was not GMT+" .. resultingHourDiff .. " ( summer time was " .. tostring(summerTime) .. ")" )
|
||||
os.exit(1)
|
||||
@ -17,3 +27,4 @@ function checkTime(yearIn, monthIn, dayIn, hourIn, minutesIn, secondsIn,dowIn, r
|
||||
print(yearIn .. "-" .. string.format("%0.2d", monthIn) .. "-" .. string.format("%0.2d", dayIn) .. " " .. string.format("%0.2d", hourIn) .. ":" .. string.format("%0.2d", minutesIn) .. ":" .. string.format("%0.2d", secondsIn) .. " summertime is " .. tostring(summerTime) .. " (GMT+" .. resultingHourDiff .. ")" )
|
||||
end
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user