Author Topic: Duration Function  (Read 1484 times)

Windlord

  • Guest
Duration Function
« on: January 11, 2011, 03:09:39 pm »
Code: (squirrel) [Select]
// This function returns a duration statement from ticks (ms)
function Duration ( ticks )
{
ticks = floor ( ticks / 1000 );
local days = floor ( ticks % 604800 / 86400 );
local hours = floor ( ticks % 86400 / 3600 );
local mins = floor ( ticks % 3600 / 60 );
local secs = ticks % 60;
local weeks = floor ( ( ticks - days*86400 - hours*3600 - mins*60 - secs ) / 604800 );
local a = [];
if ( weeks != 0 ) a.append( weeks + "wks" );
if ( days != 0 ) a.append( days + "days" );
if ( hours != 0 ) a.append( hours + "hrs" );
if ( mins != 0 ) a.append( mins + "mins" );
if ( secs != 0 ) a.append( secs + "secs" );
return JoinArray( a, " " );
}

// This function joins all elements in a 'target' array
// where the elements are delimited with a 'delimit' string
function JoinArray ( array, delimit )
{
local a, z = array.len(), output = "";
if ( z > 0 )
{
if ( z > 1 )
{
for ( a = 1; a < z; a++ )
output += delimit + array[ a ];
return array[ 0 ] + output;
}
else return array[ 0 ];
}
else return output;
}

Please post better versions if you have any!
« Last Edit: January 11, 2011, 03:30:12 pm by Windlord »

 

© Liberty Unleashed Team.