01-05-2005 17:57
string elapsed(string sTime)
----------------------------
Chromal Brodsky - 2005-01-05

elapsed() uses the highest precision clock available in LSL, llGetTimestamp(), to implement a timer with greater than one-second precision.

Typical use:

string sStart = elapsed("";);
some_function();
string sElapsed = elapsed(sStart);

At the end of the above example, sElapsed is in the format of hh:mm:ss.ssssss, e.g.: "00:01:10.605000"

CODE

// string elapsed(string sTime)
// ----------------------------
// Chromal Brodsky - 2005-01-05
//
// elapsed() uses the highest precision clock available in LSL, llGetTimestamp(),
// to implement a timer with greater than one-second precision.
//
// Typical use:
//
// string sStart = elapsed("");
// some_function();
// string sElapsed = elapsed(sStart);
//
// At the end of the above example, sElapsed is in the format of hh:mm:ss.ssssss,
// e.g.: "00:01:10.605000"
//
// On Lusk running at 1000 FPS on sim367.agni.lindenlab.com,
// I sampled the following times for { sTime = elapsed(""); llWhisper(0, elapsed(sTime) );}
//
// 00:00:00.010000 00:00:00.003000 00:00:00.003000 00:00:00.003998
// 00:00:00.002998 00:00:00.003002 00:00:00.012000 00:00:00.004001
// 00:00:00.004000 00:00:00.005001 00:00:00.003000 00:00:00.004999
// YMMV

string elapsed(string sTime)
{
if (sTime == "" )
{
sTime = llGetTimestamp();
}
else
{
string sH;
string sM;
string sS;

string sTimeEnd = llGetTimestamp();

integer iHour = (integer)llGetSubString( sTimeEnd, 11, 12 );
integer iMin = (integer)llGetSubString( sTimeEnd, 14, 15 );
float fSec = (float)llGetSubString( sTimeEnd, 17, 22 );

integer iHour_start = (integer)llGetSubString( sTime, 11, 12 );
integer iMin_start = (integer)llGetSubString( sTime, 14, 15 );
float fSec_start = (float)llGetSubString( sTime, 17, 22 );

if (iHour < iHour_start)
{
iHour += 24;
}
if (iMin < iMin_start)
{
iMin += 60;
}
if (fSec < fSec_start)
{
fSec += 60.0;
}

iHour = iHour - iHour_start;
iMin = iMin - iMin_start;
fSec = fSec - fSec_start;

if (iHour < 10)
{
sH = "0";
}
if (iMin < 10)
{
sM = "0";
}
if (fSec < 10.0)
{
sS = "0";
}

sTime = sM + (string)iHour + ":" + sM + (string)iMin + ":" + sS + (string)fSec;
}

return sTime;
}