Time lapse subroutine for seconds past or formatted duration
Here is a handy subroutine to either get the seconds past or to print out a readable period of time (this includes additional code for testing purposes):
Sample output:
Code:
Unformatted seconds passed: 216094
Formatted time passed: 2 Days, 12 Hrs, 1 Min, 34 Secs
Code:
# Return total seconds by default using Boolean value. 0 for seconds or 1 for readable duration:
setVar $_MARKTIME_FORMAT 0
# ...
goSub :TIMESTAMP
setVar $start_time $_TIMESTAMP
# e.g., test time lapse... (Note TWX appears to internally round the value up to the nearest full second.)
setDelayTrigger 1 :TEST_DURATION 8500
pause
:TEST_DURATION
killTrigger 1
goSub :TIMESTAMP
setVar $end_time $_TIMESTAMP
goSub :MARKTIME
echo "** Unformatted seconds passed: " $_MARKTIME "**"
setVar $_MARKTIME_FORMAT 1
goSub :MARKTIME
echo "** Formatted time passed: " $_MARKTIME "**"
halt
:TIMESTAMP
# Returns a timestamp decimal value.
getTime $_DATE mm:dd:yyyy
getTime $_TIME hh:nn:ss
replaceText $_DATE ":" "/"
setVar $date_time $_DATE & " " & $_TIME
format $date_time $_TIMESTAMP STRTODATETIME
return
:MARKTIME
# Returns a readable format based upon the seconds lapsed. This function is accurate only to the hole second.
setVar $_MARKTIME ""
setVar $days 0
setVar $hrs 0
setVar $mins 0
setPrecision 20
setVar $secs (($end_time-$start_time)*86400)
setPrecision 0
round $secs 0
if ($_MARKTIME_FORMAT = 1)
if ($secs > 59)
setVar $mins ($secs/60)
setVar $secs ($secs-(60*$mins))
end
if ($mins > 59)
setVar $hrs ($mins/60)
setVar $mins ($mins-(60*$hrs))
end
if ($hrs > 23)
setVar $days ($hrs/24)
setVar $hrs ($hrs-(24*$days))
end
if ($days > 0)
format $days $format NUMBER
stripText $format ".00"
if ($days > 1)
setVar $days $format & " Days"
else
setVar $days $format & " Day"
end
if ($hrs <> 0) OR ($mins <> 0) OR ($secs <> 0)
setVar $days $days & ", "
end
end
if ($hrs > 0)
if ($hrs > 1)
setVar $hrs $hrs & " Hrs"
else
setVar $hrs $hrs & " Hr"
end
if ($mins <> 0) OR ($secs <> 0)
setVar $hrs $hrs & ", "
end
end
if ($mins > 0)
if ($mins > 1)
setVar $mins $mins & " Mins"
else
setVar $mins $mins & " Min"
end
if ($secs <> 0)
setVar $mins $mins & ", "
end
end
if ($secs > 0)
if ($secs > 1)
setVar $secs $secs & " Secs"
else
setVar $secs $secs & " Sec"
end
end
if ($days <> 0)
setVar $_MARKTIME $days
end
if ($hrs <> 0)
setVar $_MARKTIME $_MARKTIME & $hrs
end
if ($mins <> 0)
setVar $_MARKTIME $_MARKTIME & $mins
end
if ($secs <> 0)
setVar $_MARKTIME $_MARKTIME & $secs
end
else
setVar $_MARKTIME $secs
end
setVar $_MARKTIME_FORMAT 0
return
halt