]> mj.ucw.cz Git - libucw.git/blob - lib/timer.c
Added missing includes to libucw API.
[libucw.git] / lib / timer.c
1 /*
2  *      UCW Library -- Execution Timing
3  *
4  *      (c) 1997 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <sys/time.h>
15
16 static struct timeval last_tv;
17
18 uns
19 get_timer(void)
20 {
21   struct timeval tv;
22   uns diff;
23
24   gettimeofday(&tv, NULL);
25   if (tv.tv_sec < last_tv.tv_sec
26       || tv.tv_sec == last_tv.tv_sec && tv.tv_usec < last_tv.tv_usec)
27     diff = 0;
28   else
29     {
30       if (tv.tv_sec == last_tv.tv_sec)
31         diff = (tv.tv_usec - last_tv.tv_usec + 500) / 1000;
32       else
33         {
34           diff = 1000 * (tv.tv_sec - last_tv.tv_sec - 1);
35           diff += (1000500 - last_tv.tv_usec + tv.tv_usec) / 1000;
36         }
37     }
38   last_tv = tv;
39   return diff;
40 }
41
42 void
43 init_timer(void)
44 {
45   gettimeofday(&last_tv, NULL);
46 }
47
48 void
49 get_last_timeval(struct timeval *tv)
50 {
51   *tv = last_tv;
52 }