/* ******************************************************************************* * Copyright (c) 1996 Martin Poole * ******************************************************************************* ** ** WARNING !! WARNING !! WARNING !! WARNING !! WARNING !! WARNING !! ** ** Any changes to be made to this file should first be checked with ** mplib1 source control for library integrity. ** ** mplib1 source control can be reached at mplib1@quatermass.co.uk ** * * * $Source: /home/cvs/cvsroot/onelan/onelan/src/mplib1/libsrc/stopwatch_c.c,v $ * $Author: mpoole $ * $Date: 2002/10/07 09:37:40 $ * $Revision: 1.2 $ * * ******************************************************************************* * * * $Log: stopwatch_c.c,v $ * Revision 1.2 2002/10/07 09:37:40 mpoole * Initial checkin of mplib1-3.1.0 * * Revision 1.1 2002/10/07 09:36:57 mpoole * Initial checkin of mplib1-3.1.0 * * ******************************************************************************* */ #ident "$Header: /home/cvs/cvsroot/onelan/onelan/src/mplib1/libsrc/stopwatch_c.c,v 1.2 2002/10/07 09:37:40 mpoole Exp $" /* ------------------------------------------------------------------ Include files ------------------------------------------------------------------ */ #include #include #include #include #include #include #include #include #include #include /* ------------------------------------------------------------------ defines ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ Code starts here ------------------------------------------------------------------ */ void diff_timevals( struct timeval *end_p, struct timeval *start_p ) { /* Generate the difference, subtract the start from the end */ end_p->tv_sec -= start_p->tv_sec; if (end_p->tv_usec < start_p->tv_usec) { end_p->tv_usec = (end_p->tv_usec + 1000000 - start_p->tv_usec); end_p->tv_sec--; }else { end_p->tv_usec -= start_p->tv_usec; } return; } void add_timevals( struct timeval *cump, struct timeval *tvp ) { if (cump && tvp) { /* Now add it in */ cump->tv_usec += tvp->tv_usec; if (cump->tv_usec >= 1000000) { cump->tv_usec -= 1000000; cump->tv_sec++; } cump->tv_sec += tvp->tv_sec; } return; } void accum_stop_timevals( struct stopwatch *stp, struct timeval *etp ) { /* get the difference between the start and end time */ diff_timevals( etp, &stp->start_time ); /* Now add it in */ add_timevals( &stp->cumulative_time, etp ); return; } char * display_stopwatch( struct stopwatch *stp, int mode ) { int sub; struct tm *tm; switch( mode & STOPWATCH_BASE_SMASK ) { case STOPWATCH_BASE_SECS : if (stp->cumulative_time.tv_sec < 1000) { sub = sprintf( stp->elps_str, "%03d", (int)stp->cumulative_time.tv_sec ); }else { sub = sprintf( stp->elps_str, "%06d", (int)stp->cumulative_time.tv_sec ); } break; case STOPWATCH_BASE_DAYS : default : tm = gmtime( &stp->cumulative_time.tv_sec ); sub = snprintf( stp->elps_str, STOPWATCH_ELPS_SIZE, "%d ", (int)stp->cumulative_time.tv_sec / SECS_PER_DAY ); sub += strftime( stp->elps_str+sub, (size_t)(STOPWATCH_ELPS_SIZE-sub-1), "%H:%M:%S", tm ); } switch( mode & STOPWATCH_BASE_FMASK ) { case STOPWATCH_SECS_CENTIS : sprintf( stp->elps_str+sub, ".%02d seconds", (int)stp->cumulative_time.tv_usec/10000 ); break; case STOPWATCH_SECS_MILLIS : sprintf( stp->elps_str+sub, ".%03d seconds", (int)stp->cumulative_time.tv_usec/1000 ); break; case STOPWATCH_SECS_MICROS : default : sprintf( stp->elps_str+sub, ".%06d seconds", (int)stp->cumulative_time.tv_usec ); break; } return(stp->elps_str); } /* -- End of File -- */