Get elapsed time between two timestamps

Find out how to get things done with Servoy. Post how YOU get things done with Servoy

Get elapsed time between two timestamps

Postby friedrich toussaint » Tue Jan 04, 2005 9:37 am

a happy new year to everybody,

with look for the causes of performance problems I found the posting of bcusic:
http://forum.servoy.com/viewtopic.php?t ... ht=elapsed

the script works great, but i needed it somewhere more flexible: here is my version:

there are four arguments, syntax looks like this:

elements.result.txt = globals.getElapsed(globals.gStartDate, globals.gEndDate, 'txt', 'DD');
elements.result.int = globals.getElapsed(globals.gStartDate, globals.gEndDate, 'int', ';');
elements.result.num = globals.getElapsed(globals.gStartDate, globals.gEndDate, 'num', 'HH');


the code:
//******************************************************************************************************************************************
// get arguments
//******************************************************************************************************************************************
var beg = arguments[0]; // start date
var end = arguments[1]; // end date
var ret_type = arguments[2]; // the return data type
var ret_format = arguments[3]; // the return data format
//******************************************************************************************************************************************
/*
arg1 = startdate
arg2 = enddate
arg3 = return data type, 'txt', 'int' or 'num'
arg4 = return data fornat,
at arg3 = 'txt' : the bigest returned format based on,
e.g.: 'HH' returns 'HH:mm:ss:SSS' or
'DD' returns 'DD HH:mm:ss:SSS' or
'ss' returns 'ss:SSS'
at arg3 = 'int' : define ret_format delimiter
e.g: ';' returns the array 'DD;HH;mm;ss;ms'
at arg3 = 'num' : define ret_format you want to return,
e.g.: 'DD', 'HH', 'mm', 'ss' or 'SSS'
*/
//******************************************************************************************************************************************
// calc difference (milliseconds)
//******************************************************************************************************************************************
var dif = Math.ceil(end - beg);
//******************************************************************************************************************************************
// calc returns number
//******************************************************************************************************************************************
var days = dif / 86400000;
var hours = dif / 3600000;
var minutes = dif / 60000;
var seconds = dif / 1000;
var milliseconds = dif / 1;
//******************************************************************************************************************************************
// calc returns int for can do DD HH-mm-ss-ms
//******************************************************************************************************************************************
var int_days = Math.abs(dif / 86400000);
var rest_days = dif % 86400000;
var int_hours = Math.abs(rest_days / 3600000);
var rest_hours = rest_days % 3600000;
var int_minutes = Math.abs(rest_hours /60000);
var rest_minutes = rest_hours % 60000;
var int_seconds = Math.abs(rest_minutes / 1000);
var rest_seconds = rest_minutes % 1000;
//******************************************************************************************************************************************
// calc return num/txt for can do DD HH-mm-ss-SSS
//******************************************************************************************************************************************
if (days == 0)
{
var numD = '000';
}
else if (days < 10)
{
var numD = '00' + utils.numberFormat(Math.floor(days), 0);
}
else if (days < 100)
{
var numD = '0' + utils.numberFormat(Math.floor(days), 0);
}
else
{
var numD = utils.numberFormat(Math.floor(days), 0);
}
if (hours == 0)
{
var numH = '00';
}
else if (hours < 10)
{
var numH = '0' + utils.numberFormat(Math.floor(hours), 0);
}
else
{
var numH = utils.numberFormat(Math.floor(hours), 0);
}
if (minutes == 0)
{
var numM = '00';
}
else if (minutes < 10)
{
var numM = '0' + utils.numberFormat(Math.floor(minutes), 0);
}
else
{
var numM = utils.numberFormat(Math.floor(minutes), 0);
}
if (seconds == 0)
{
var numS = '00';
}
else if (seconds < 10)
{
var numS = '0' + utils.numberFormat(Math.floor(seconds), 0);
}
else
{
var numS = utils.numberFormat(Math.floor(seconds), 0);
}
if (milliseconds == 0)
{
var numMS = '0000';
}
else if (milliseconds < 10)
{
var numMS = '00' + utils.numberFormat(milliseconds, 0);
}
else if (milliseconds < 100)
{
var numMS = '0' + utils.numberFormat(milliseconds, 0);
}
else
{
var numMS = utils.numberFormat(milliseconds, 0);
}
//******************************************************************************************************************************************
// calc return int/txt for can do DD HH-mm-ss-SSS
//******************************************************************************************************************************************
if (int_days == 0)
{
var intD = '000';
}
else if (int_days < 10)
{
var intD = '00' + utils.numberFormat(Math.floor(int_days), 0);
}
else if (days < 100)
{
var intD = '0' + utils.numberFormat(Math.floor(int_days), 0);
}
else
{
var intD = utils.numberFormat(Math.floor(int_days), 0);
}
if (int_hours == 0)
{
var intH = '00';
}
else if (int_hours < 10)
{
var intH = '0' + utils.numberFormat(Math.floor(int_hours), 0);
}
else
{
var intH = utils.numberFormat(Math.floor(int_hours), 0);
}
if (int_minutes == 0)
{
var intM = '00';
}
else if (int_minutes < 10)
{
var intM = '0' + utils.numberFormat(Math.floor(int_minutes), 0);
}
else
{
var intM = utils.numberFormat(Math.floor(int_minutes), 0);
}
if (int_seconds == 0)
{
var intS = '00';
}
else if (int_seconds < 10)
{
var intS = '0' + utils.numberFormat(Math.floor(int_seconds), 0);
}
else
{
var intS = utils.numberFormat(Math.floor(int_seconds), 0);
}
if (rest_seconds == 0)
{
var intMS = '000';
}
else if (rest_seconds < 10)
{
var intMS = '00' + utils.numberFormat(rest_seconds, 0);
}
else if (rest_seconds < 100)
{
var intMS = '0' + utils.numberFormat(rest_seconds, 0);
}
else
{
var intMS = utils.numberFormat(rest_seconds, 0);
}
//******************************************************************************************************************************************
// check ret_type
//******************************************************************************************************************************************
switch( ret_type )
{
case 'txt' : if (!ret_format)
{
plugins.dialogs.showErrorDialog( 'Error', 'err#0002: type txt, no return format defined!', 'OK');
}
else
{
switch(ret_format)
{
case 'DD' : var ret_calc = 'DD HH:mm:ss,SSS';
ret_calc = utils.stringReplace(ret_calc, 'DD', numD);
ret_calc = utils.stringReplace(ret_calc, 'HH', intH);
ret_calc = utils.stringReplace(ret_calc, 'mm', intM);
ret_calc = utils.stringReplace(ret_calc, 'ss', intS);
ret_calc = utils.stringReplace(ret_calc, 'SSS', intMS);
break;
case 'HH' : var ret_calc = 'HH:mm:ss:SSS';
ret_calc = utils.stringReplace(ret_calc, 'HH', numH);
ret_calc = utils.stringReplace(ret_calc, 'mm', intM);
ret_calc = utils.stringReplace(ret_calc, 'ss', intS);
ret_calc = utils.stringReplace(ret_calc, 'SSS', intMS);
break;
case 'mm' : var ret_calc = 'mm:ss:SSS';
ret_calc = utils.stringReplace(ret_calc, 'mm', numM);
ret_calc = utils.stringReplace(ret_calc, 'ss', intS);
ret_calc = utils.stringReplace(ret_calc, 'SSS', intMS);
break;
case 'ss' : var ret_calc = 'ss:SSS';
ret_calc = utils.stringReplace(ret_calc, 'ss', numS);
ret_calc = utils.stringReplace(ret_calc, 'SSS', intMS);
break;
case 'SSS' : var ret_calc = 'SSS';
ret_calc = utils.stringReplace(ret_calc, 'SSS', numMS);
break;
default : plugins.dialogs.showErrorDialog( 'Error', 'err#0002: type txt, wrong return format defined!', 'OK');
}
}
break;
case 'int' :if (!ret_format)
{
plugins.dialogs.showErrorDialog( 'Error', 'err#0003: type int, no return format defined!', 'OK');
}
else
{
var ret_calc = 'DD' + ret_format + 'HH' + ret_format + 'mm' + ret_format + 'ss' + ret_format + 'SSS';
ret_calc = utils.stringReplace(ret_calc, 'DD', utils.numberFormat(int_days, 0));
ret_calc = utils.stringReplace(ret_calc, 'HH', utils.numberFormat(int_hours, 0));
ret_calc = utils.stringReplace(ret_calc, 'mm', utils.numberFormat(int_minutes, 0));
ret_calc = utils.stringReplace(ret_calc, 'ss', utils.numberFormat(int_seconds, 0));
ret_calc = utils.stringReplace(ret_calc, 'SSS', utils.numberFormat(rest_seconds, 0));
}
break;
case 'num' : var ret_array = ret_format.split(',',2);
switch( ret_array[0])
{
case 'DD' : var ret_calc = days;
break;
case 'HH' : var ret_calc = hours;
break;
case 'mm' : var ret_calc = minutes;
break;
case 'ss' : var ret_calc = seconds;
break;
case 'SSS' : var ret_calc = milliseconds;
break;
default : plugins.dialogs.showErrorDialog( 'Error', 'err#0004: type num, no or wrong return format defined!', 'OK');
}
break;
default: plugins.dialogs.showErrorDialog( 'Error', 'err#0001: no return type defined!', 'OK');
}
//******************************************************************************************************************************************
// return calculation
//******************************************************************************************************************************************
return ret_calc;
//******************************************************************************************************************************************

have a nice day
your friedrich toussaint
friedrich toussaint
 
Posts: 31
Joined: Tue Aug 03, 2004 1:32 pm
Location: Linz, Austria

Postby bcusick » Wed Jan 05, 2005 11:29 pm

Friedrich,

Thanks for sharing this!

Nice work!
Bob Cusick
bcusick
 
Posts: 1255
Joined: Wed Apr 23, 2003 11:27 pm
Location: Thousand Oaks, CA USA


Return to How To

Who is online

Users browsing this forum: No registered users and 4 guests

cron