Fixed leap time calculation; tested unixtimestamp convertion for the years 2016 to 2020
This commit is contained in:
parent
2dccb0e09a
commit
3deaea6709
28
timecore.lua
28
timecore.lua
@ -18,7 +18,7 @@ local TIMEZONE_OFFSET_WINTER = 1
|
|||||||
--setclockto 3 am;
|
--setclockto 3 am;
|
||||||
--DST=1;
|
--DST=1;
|
||||||
--}
|
--}
|
||||||
function getLocalTime(year, month, day, hour, minutes, seconds,dow)
|
function getLocalTime(year, month, day, hour, minutes, second,dow)
|
||||||
-- Always adapte the timezoneoffset:
|
-- Always adapte the timezoneoffset:
|
||||||
hour = hour + (TIMEZONE_OFFSET_WINTER)
|
hour = hour + (TIMEZONE_OFFSET_WINTER)
|
||||||
|
|
||||||
@ -41,7 +41,7 @@ function getLocalTime(year, month, day, hour, minutes, seconds,dow)
|
|||||||
hour = hour + 1
|
hour = hour + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
return year, month, day, hour, minutes, seconds
|
return year, month, day, hour, minutes, second
|
||||||
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.
|
||||||
@ -117,11 +117,11 @@ ytab[1][10] = 30
|
|||||||
ytab[1][11] = 31
|
ytab[1][11] = 31
|
||||||
|
|
||||||
|
|
||||||
leapyear = function(year)
|
local leapyear = function(year)
|
||||||
return ( not ((year) % 4) and (((year) % 100) or not ((year) % 400)))
|
return ( not ((year) % 4 ~= 0) and (((year) % 100 ~= 0) or not ((year) % 400 ~= 0)))
|
||||||
end
|
end
|
||||||
|
|
||||||
yearsize = function(year)
|
local yearsize = function(year)
|
||||||
if leapyear(year) then
|
if leapyear(year) then
|
||||||
return 366
|
return 366
|
||||||
else
|
else
|
||||||
@ -132,9 +132,9 @@ end
|
|||||||
gettime = function(unixtimestmp)
|
gettime = function(unixtimestmp)
|
||||||
local year = EPOCH_YR
|
local year = EPOCH_YR
|
||||||
local dayclock = math.floor(unixtimestmp % SECS_DAY)
|
local dayclock = math.floor(unixtimestmp % SECS_DAY)
|
||||||
local dayno = math.floor(unixtimestmp / SECS_DAY)
|
dayno = math.floor(unixtimestmp / SECS_DAY)
|
||||||
|
|
||||||
local sec = dayclock % 60
|
local sec = math.floor(dayclock % 60)
|
||||||
local min = math.floor( (dayclock % 3600) / 60)
|
local min = math.floor( (dayclock % 3600) / 60)
|
||||||
local hour = math.floor(dayclock / 3600)
|
local hour = math.floor(dayclock / 3600)
|
||||||
local wday = math.floor( (dayno + 4) % 7) -- Day 0 was a thursday
|
local wday = math.floor( (dayno + 4) % 7) -- Day 0 was a thursday
|
||||||
@ -144,19 +144,17 @@ gettime = function(unixtimestmp)
|
|||||||
dayno = dayno - yearsize(year);
|
dayno = dayno - yearsize(year);
|
||||||
year=year + 1
|
year=year + 1
|
||||||
end
|
end
|
||||||
local yday = dayno
|
--Day in whole year: local yday = dayno (Not needed)
|
||||||
local mon = 0
|
mon = 0
|
||||||
|
isleap = leapyear(year) and 1 or 0
|
||||||
isleap=0 if leapyear(year) then isleap=1 end
|
while (dayno >= ytab[leapyear(year) and 1 or 0][mon])
|
||||||
while (dayno >= ytab[isleap][mon])
|
|
||||||
do
|
do
|
||||||
dayno = dayno - ytab[isleap][mon];
|
dayno = dayno - ytab[leapyear(year) and 1 or 0][mon];
|
||||||
mon = mon + 1
|
mon = mon + 1
|
||||||
isleap=0 if leapyear(year) then isleap=1 end
|
|
||||||
end
|
end
|
||||||
mday = dayno + 1
|
mday = dayno + 1
|
||||||
|
|
||||||
return year, mon, mday, hour, min, sec, wday
|
return year, (mon+1), mday, hour, min, sec, wday
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
52
unit/generateUnixtimes.sh
Executable file
52
unit/generateUnixtimes.sh
Executable file
@ -0,0 +1,52 @@
|
|||||||
|
#/bin/bash
|
||||||
|
|
||||||
|
# date command is all we need, as shown:
|
||||||
|
#date -d '2007-011-01 17:30:24' '+%s'
|
||||||
|
#1193934624
|
||||||
|
#date -d '2007-011-01 17:30:24' '+%w'
|
||||||
|
#4
|
||||||
|
|
||||||
|
# Usage:
|
||||||
|
# Call this script and write the output of stdout into a file.
|
||||||
|
# e.g.: ./generateUnixtime.sh > unixtimetest.lua
|
||||||
|
|
||||||
|
# Generate the header
|
||||||
|
cat << EOF
|
||||||
|
dofile("../timecore.lua")
|
||||||
|
function checkUnixTime(resultYear, resultMonth, resultDay, resultHour, resultMinute, resultSecond, resultDow, unixtime)
|
||||||
|
year, month, day, hour, minute, second, dow = gettime(unixtime)
|
||||||
|
|
||||||
|
if not (year == resultYear and resultMonth == month and day == resultDay and hour == resultHour and minute == resultMinute and second == resultSecond and dow == resultDow) then
|
||||||
|
print(resultYear .. "-" .. resultMonth .. "-" .. resultDay .. " " .. resultHour .. ":" .. resultMinute .. ":" .. resultSecond .. " day of week : " .. resultDow .. " not extracted from " .. unixtime)
|
||||||
|
print(year .. "-" .. month .. "-" .. day .. " " .. hour .. ":" .. minute .. ":" .. second .. " day of week : " .. dow .. " found instead")
|
||||||
|
os.exit(1)
|
||||||
|
else
|
||||||
|
print(resultYear .. "-" .. resultMonth .. "-" .. resultDay .. " " .. resultHour .. ":" .. resultMinute .. ":" .. resultSecond .. " OK")
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
|
EOF
|
||||||
|
|
||||||
|
# Generate all the tests
|
||||||
|
for year in {2016..2020}; do
|
||||||
|
for month in {1..12}; do
|
||||||
|
for day in {1..31}; do
|
||||||
|
hour=$((RANDOM%24))
|
||||||
|
minutes=$((RANDOM%60))
|
||||||
|
seconds=$((RANDOM%60))
|
||||||
|
timestmp="$year-$month-$day $hour:$minutes:$seconds"
|
||||||
|
date -d "$timestmp" "+%F %T" >> /dev/null
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "--Time $timestmp is not valid"
|
||||||
|
else
|
||||||
|
unixtime=$(date -u -d "$timestmp" '+%s')
|
||||||
|
dayofweek=$(date -u -d "$timestmp" '+%w')
|
||||||
|
# Generate the lua test command, like: checkTime(2015, 1, 1, 10, 11, 12, 0, 1)
|
||||||
|
echo "checkUnixTime($year, $month, $day, $hour, $minutes, $seconds, $dayofweek, $unixtime)"
|
||||||
|
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
done
|
||||||
|
done
|
||||||
|
exit 0
|
1874
unit/testUnixtimes.lua
Normal file
1874
unit/testUnixtimes.lua
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user