Timestamp difference
It’s a heavy (nasty code) function so use it with caution (not too often) but for rare occasions and small differences it should to the trick getting the timestamp difference as number.
You can increase the possible time differences for the cost of accuracy using nDigitPrecision:
nDigitPrecision: 0 (accurate to 1 second) -> max diff: 11.5 days nDigitPrecision: 1 (accurate to 10 seconds) -> max diff: 115 days nDigitPrecision: 2 (accurate to 100 seconds) -> max diff: 1150 days nDigitPrecision: 3 (accurate to 1000 seconds) -> max diff: 11500 days nDigitPrecision: …
Warning: changing nDigitPrecision will also make the output values smaller (e.g: if nDigitPrecision = 1 and the real difference is 1234 you will get 123 as return value).
Note: This only works for “small” time differences.
--------------------------------------------------------------------------------
function YourAI.getUnixTimestampDiffAsNumber ( sUnixTimestampA, sUnixTimestampB, nDigitPrecision )
--------------------------------------------------------------------------------
if(nDigitPrecision == nil)
then
nDigitPrecision = 0 -- digits that will be cut of at the end e.g. "123456" with precision "1" becomes "12345"
end
if(string.getLength ( sUnixTimestampA ) ~= string.getLength ( sUnixTimestampB ) )
then
log.warning ( "Timestamps A und B length differs: can not compare them -> abort")
return nil
end
if(string.compare ( sUnixTimestampA, sUnixTimestampB ) == 0)
then
return 0
end
local nLength = string.getLength ( sUnixTimestampA )
local sDigitA
local sDigitB
local nDifferenceFoundAt = -1
local sResultA = ""
local sResultB = ""
local nResultA = 0
local nResultB = 0
-- cut digits
if(nDigitPrecision > 0)
then
sUnixTimestampA = string.getSubString ( sUnixTimestampA, 0, nLength - nDigitPrecision )
sUnixTimestampB = string.getSubString ( sUnixTimestampB, 0, nLength - nDigitPrecision )
end
-- find start of difference
for i=0, nLength-1
do
sDigitA = string.getSubString ( sUnixTimestampA, i, 1 )
sDigitB = string.getSubString ( sUnixTimestampB, i, 1 )
if(string.compare ( sDigitA, sDigitB ) ~= 0)
then
nDifferenceFoundAt = i
break
end
end
if(nDifferenceFoundAt == -1)
then
return 0
else
sResultA = string.getSubString ( sUnixTimestampA, nDifferenceFoundAt, nLength - nDifferenceFoundAt )
sResultB = string.getSubString ( sUnixTimestampB, nDifferenceFoundAt, nLength - nDifferenceFoundAt )
end
-- if difference is too big return error
if(string.getLength ( sResultA ) == 0 or string.getLength ( sResultA ) > 7)
then
log.warning ( "Time Difference is too ig for StoneScript LUA: " .. sResult )
return nil
end
if(string.getLength ( sResultB ) == 0 or string.getLength ( sResultB ) > 7)
then
log.warning ( "Time Difference is too ig for StoneScript LUA: " .. sResult )
return nil
end
nResultA = string.toNumber ( sResultA )
nResultB = string.toNumber ( sResultB )
return nResultB - nResultA -- assume a is die lower value
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------