Refactored summer-time calcuating function. Now, only the state summertime or wintertime is returned

This commit is contained in:
ollo 2016-05-15 15:44:19 +02:00
parent 4793f3fe0e
commit 8bb594d190
5 changed files with 126490 additions and 126477 deletions

View File

@ -1,6 +1,3 @@
-- As we are located in central europe, we have GMT+1
local TIMEZONE_OFFSET_WINTER = 1
--Summer winter time convertion --Summer winter time convertion
--See: https://arduinodiy.wordpress.com/2015/10/13/the-arduino-and-daylight-saving-time/ --See: https://arduinodiy.wordpress.com/2015/10/13/the-arduino-and-daylight-saving-time/
-- --
@ -18,30 +15,36 @@ local TIMEZONE_OFFSET_WINTER = 1
--setclockto 3 am; --setclockto 3 am;
--DST=1; --DST=1;
--} --}
function getLocalTime(year, month, day, hour, minutes, second,dow)
-- Always adapte the timezoneoffset:
hour = hour + (TIMEZONE_OFFSET_WINTER)
-- @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)
function isSummerTime(year, month, day, hour, minute, second,dow)
-- we are in 100% in the summer time -- we are in 100% in the summer time
if (month > 3 and month < 10) then if (month > 3 and month < 10) then
hour = hour + 1 return true
-- March is not 100% Summer time, only starting at the last sunday -- 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 ((month == 3 and day >= 25 and day <= 31 and hour > 2 and dow == 7) or
-- Only handle days after the last sunday in this month -- 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 ((month == 3 and day >= 25 and day <= 31 and dow < 7 and ((7-dow + day) > 31))) ) then
-- set summer time -- summer time
hour = hour + 1 return true
-- October is not 100% Summer time, ending with the last sunday -- October is not 100% Summer time, ending with the last sunday
elseif ((month == 10 and day >= 25 and day <= 31 and hour <= 2 and dow == 7) or 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 (month == 10 and day >= 25 and day <= 31 and dow < 7 and ((7-dow + day) <= 31)) or
-- Handle all days up to the 25. of october -- Handle all days up to the 25. of october
(month == 10 and day < 25 ) (month == 10 and day < 25 )
)then )then
-- set summer time -- summer time
hour = hour + 1 return true
end end
-- otherwise it must be winter time
return year, month, day, hour, minutes, second return false
end end
-- Convert a given month in english abr. to number from 1 to 12. -- Convert a given month in english abr. to number from 1 to 12.
@ -129,7 +132,7 @@ local yearsize = function(year)
end end
end end
gettime = function(unixtimestmp) function getUTCtime(unixtimestmp)
local year = EPOCH_YR local year = EPOCH_YR
local dayclock = math.floor(unixtimestmp % SECS_DAY) local dayclock = math.floor(unixtimestmp % SECS_DAY)
dayno = math.floor(unixtimestmp / SECS_DAY) dayno = math.floor(unixtimestmp / SECS_DAY)

View File

@ -39,4 +39,8 @@ for year in {2016..2100}; do
done done
cat $TAIL >> $OUTPUT cat $TAIL >> $OUTPUT
echo "---------------------------"
echo "Usage:"
echo "Call lua $OUTPUT in the terminal to execute the test"
exit 0 exit 0

File diff suppressed because it is too large Load Diff

View File

@ -2,14 +2,18 @@
dofile("../timecore.lua") dofile("../timecore.lua")
function checkTime(yearIn, monthIn, dayIn, hourIn, minutesIn, secondsIn,dowIn, resultingHourDiff) SUMMERTIME_OFFSET=2
year, month, day, hour, minutes, seconds = getLocalTime(yearIn, monthIn, dayIn, hourIn, minutesIn, secondsIn, dowIn) WINTERTIME_OFFSET=1
if not (year == yearIn and monthIn == month and day == dayIn and hour == (hourIn + resultingHourDiff) and minutesIn == minutes and secondsIn == seconds ) then -- This functions expects to be executed in Germany,
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) .. " (" .. dowIn .. ".th dayOfWeek) not as expected GMT+" .. resultingHourDiff .. " but GMT+" .. (hour - hourIn) ) -- 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)
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) os.exit(1)
else else
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) .. " is GMT+" .. resultingHourDiff ) 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
end end

View File

@ -1,2 +0,0 @@
dofile("../timecore.lua")
print( gettime(1462391501) )