3 // Copyright 2009 Michael Cronenworth <mike@cchtml.com>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 // MA 02110-1301, USA.
28 static long int timerStartTime = 0;
29 static long int timerSave = 0;
32 static int timerPrecision = TIMER_PRECISION_MINUTE;
35 char *stopish_get_time_string( void )
42 currentTime = stopish_current_time( );
44 // calculate time format
45 ss = ( currentTime - timerStartTime ) % 10;
46 s = ( currentTime - timerStartTime ) / 10;
52 if ( timerPrecision == TIMER_PRECISION_MINUTE && m > 60 ) {
53 // rollover once we hit one hour
54 stopish_set_time_start( stopish_current_time( ) );
57 else if ( timerPrecision == TIMER_PRECISION_HOUR && h > 24 ) {
58 // rollover once we hit one day
59 stopish_set_time_start( stopish_current_time( ) );
63 timeBuffer = malloc( 64 );
64 if ( timerPrecision == TIMER_PRECISION_MINUTE )
65 sprintf( timeBuffer, "%.02d:%.02d.%.1d",
68 sprintf( timeBuffer, "%.02d:%.02d:%.02d.%.1d",
75 void stopish_set_time_start( long int time )
77 // reset timer to user-inputted time
78 timerStartTime = time;
83 void stopish_timer_resume( void )
85 timerStartTime = stopish_current_time( );
86 timerStartTime -= timerSave;
90 void stopish_timer_save( void )
93 timerSave = stopish_current_time( ) - timerStartTime;
97 long int stopish_current_time( void )
102 gettimeofday( &tv, NULL );
103 s = tv.tv_sec % 100000;
104 us = tv.tv_usec / 100000;
106 return ( s * 10 + us );
110 void stopish_timer_set_precision( int setting )
112 timerPrecision = setting;
116 int stopish_timer_get_precision( void )
118 return timerPrecision;