]> mj.ucw.cz Git - libucw.git/blob - ucw/time-stamp.c
Conf: Introduced cf_set_journalling()
[libucw.git] / ucw / time-stamp.c
1 /*
2  *      UCW Library -- A Simple Millisecond Timer
3  *
4  *      (c) 2007--2012 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 #include <ucw/time.h>
12
13 #include <sys/time.h>
14 #include <time.h>
15
16 #ifdef CONFIG_UCW_MONOTONIC_CLOCK
17
18 timestamp_t
19 get_timestamp(void)
20 {
21   struct timespec ts;
22   if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
23     die("clock_gettime failed: %m");
24   return (timestamp_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
25 }
26
27 #else
28
29 timestamp_t
30 get_timestamp(void)
31 {
32   struct timeval tv;
33   gettimeofday(&tv, NULL);
34   return (timestamp_t)tv.tv_sec * 1000 + tv.tv_usec / 1000
35 #ifdef CONFIG_UCW_DEBUG
36         + 3141592653    // So that we catch all attempts to corelate timestamp_t with wall clock
37 #endif
38         ;
39 }
40
41 #endif
42
43 #ifdef TEST
44
45 #include <stdio.h>
46
47 int main(void)
48 {
49   printf("%ju\n", (intmax_t) get_timestamp());
50   return 0;
51 }
52
53 #endif