Operating System - HP-UX
1833042 Members
2316 Online
110049 Solutions
New Discussion

Date conversion and calculating elapsed times

 
Mike Duffy
Frequent Advisor

Date conversion and calculating elapsed times

Hi,

Does anyone know how to convert a date/time into seconds and then calculate the elapsed time between two times?

Thanks in advance.
5 REPLIES 5
Kenneth_19
Trusted Contributor

Re: Date conversion and calculating elapsed times

I have a similar program in C which might fit you, it turns two dates into seconds and compare their difference:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include
#include
#include
#include
#include

int main(argc, argv)
int argc;
char *argv[];
{
struct tm st_time, en_time;
time_t start, finish;
double diff_time;

if (argc != 13)
{
printf("usage: timediff fm_yr fm_mon fm_day fm_hr fm_min fm_sec to_yr to_mon to_day to_hr to_min to_sec\n");
printf(" where fm_yr and to_yr are years in 4 digits\n");
printf(" and fm_hr and to_hr are hours in 24 hours time.\n\n");
printf("timediff returns the difference of two inputs in seconds\n");
}
else
{

st_time.tm_year = atoi(argv[1])-1900;
st_time.tm_mon = atoi(argv[2])-1;
st_time.tm_mday = atoi(argv[3]);
st_time.tm_hour = atoi(argv[4]);
st_time.tm_min = atoi(argv[5]);
st_time.tm_sec = atoi(argv[6]);
st_time.tm_isdst = 0;

en_time.tm_year = atoi(argv[7])-1900;
en_time.tm_mon = atoi(argv[8])-1;
en_time.tm_mday = atoi(argv[9]);
en_time.tm_hour = atoi(argv[10]);
en_time.tm_min = atoi(argv[11]);
en_time.tm_sec = atoi(argv[12]);
en_time.tm_isdst = 0;

finish = mktime(&en_time);
start = mktime(&st_time);
/*
printf("%ld, %s", start, ctime(&start));
printf("%ld, %s", finish, ctime(&finish));
*/
diff_time = difftime(finish,start);
/*
printf( "\nDelay time = %6.0f seconds.\n", diff_time );
*/
printf( "%-6.0f\n",diff_time );
}
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Always take care of your dearest before it is too late
Mike Duffy
Frequent Advisor

Re: Date conversion and calculating elapsed times

Hi,

Somethink in Korn shell would be nice.
Pete Randall
Outstanding Contributor

Re: Date conversion and calculating elapsed times

You should take a look at A Clay Stephenson's "date hammer". A boolean forum search on +"date" +"hammer" +"Clay" turned up many threads, one of which is:
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xea948cc5e03fd6118fff0090279cd0f9,00.html

Hope This Helps,
Pete

Pete
nanthakishore
Occasional Advisor

Re: Date conversion and calculating elapsed times


start_time=$SECONDS
sleep 5
end_time=$SECONDS

elapsed_time=expr $start_time - $end_time`

echo $elapsed_time
nanthakishore
Occasional Advisor

Re: Date conversion and calculating elapsed times

Sorry,

it should be like this,
elapsed_time=expr $end_time - $start_time`