]> mj.ucw.cz Git - libucw.git/blob - ucw/timer.c
The big move. Step #1: Move whole lib/ to ucw/.
[libucw.git] / ucw / timer.c
1 /*
2  *      UCW Library -- A Simple Millisecond Timer
3  *
4  *      (c) 2007 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 "ucw/lib.h"
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <sys/time.h>
15
16 timestamp_t
17 get_timestamp(void)
18 {
19   struct timeval tv;
20   gettimeofday(&tv, NULL);
21   return (timestamp_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
22 }
23
24 void
25 init_timer(timestamp_t *timer)
26 {
27   *timer = get_timestamp();
28 }
29
30 uns
31 get_timer(timestamp_t *timer)
32 {
33   timestamp_t t = *timer;
34   *timer = get_timestamp();
35   return MIN(*timer-t, ~0U);
36 }
37
38 uns
39 switch_timer(timestamp_t *old, timestamp_t *new)
40 {
41   *new = get_timestamp();
42   return MIN(*new-*old, ~0U);
43 }