]>
Commit | Line | Data |
---|---|---|
7790a2d6 DL |
1 | /* |
2 | * Copyright (c) 2017 David Lamparter, for NetDEF, Inc. | |
3 | * | |
4 | * Permission to use, copy, modify, and distribute this software for any | |
5 | * purpose with or without fee is hereby granted, provided that the above | |
6 | * copyright notice and this permission notice appear in all copies. | |
7 | * | |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
15 | */ | |
16 | ||
17 | #ifndef _FRR_MONOTIME_H | |
18 | #define _FRR_MONOTIME_H | |
19 | ||
20 | #include <stdint.h> | |
21 | #include <time.h> | |
22 | #include <sys/time.h> | |
23 | ||
5e244469 RW |
24 | #ifdef __cplusplus |
25 | extern "C" { | |
26 | #endif | |
27 | ||
7790a2d6 DL |
28 | #ifndef TIMESPEC_TO_TIMEVAL |
29 | /* should be in sys/time.h on BSD & Linux libcs */ | |
d62a17ae | 30 | #define TIMESPEC_TO_TIMEVAL(tv, ts) \ |
31 | do { \ | |
32 | (tv)->tv_sec = (ts)->tv_sec; \ | |
33 | (tv)->tv_usec = (ts)->tv_nsec / 1000; \ | |
34 | } while (0) | |
7790a2d6 | 35 | #endif |
39cea822 DL |
36 | #ifndef TIMEVAL_TO_TIMESPEC |
37 | /* should be in sys/time.h on BSD & Linux libcs */ | |
d62a17ae | 38 | #define TIMEVAL_TO_TIMESPEC(tv, ts) \ |
39 | do { \ | |
40 | (ts)->tv_sec = (tv)->tv_sec; \ | |
41 | (ts)->tv_nsec = (tv)->tv_usec * 1000; \ | |
42 | } while (0) | |
39cea822 | 43 | #endif |
7790a2d6 DL |
44 | |
45 | static inline time_t monotime(struct timeval *tvo) | |
46 | { | |
d62a17ae | 47 | struct timespec ts; |
7790a2d6 | 48 | |
d62a17ae | 49 | clock_gettime(CLOCK_MONOTONIC, &ts); |
50 | if (tvo) { | |
51 | TIMESPEC_TO_TIMEVAL(tvo, &ts); | |
52 | } | |
53 | return ts.tv_sec; | |
7790a2d6 DL |
54 | } |
55 | ||
99a6a31e DS |
56 | #define ONE_DAY_SECOND 60*60*24 |
57 | #define ONE_WEEK_SECOND ONE_DAY_SECOND*7 | |
58 | #define ONE_YEAR_SECOND ONE_DAY_SECOND*365 | |
59 | ||
7790a2d6 DL |
60 | /* the following two return microseconds, not time_t! |
61 | * | |
62 | * also, they're negative forms of each other, but having both makes the | |
63 | * code more readable | |
64 | */ | |
65 | static inline int64_t monotime_since(const struct timeval *ref, | |
d62a17ae | 66 | struct timeval *out) |
7790a2d6 | 67 | { |
d62a17ae | 68 | struct timeval tv; |
69 | monotime(&tv); | |
70 | timersub(&tv, ref, &tv); | |
71 | if (out) | |
72 | *out = tv; | |
73 | return (int64_t)tv.tv_sec * 1000000LL + tv.tv_usec; | |
7790a2d6 DL |
74 | } |
75 | ||
76 | static inline int64_t monotime_until(const struct timeval *ref, | |
d62a17ae | 77 | struct timeval *out) |
7790a2d6 | 78 | { |
d62a17ae | 79 | struct timeval tv; |
80 | monotime(&tv); | |
81 | timersub(ref, &tv, &tv); | |
82 | if (out) | |
83 | *out = tv; | |
84 | return (int64_t)tv.tv_sec * 1000000LL + tv.tv_usec; | |
7790a2d6 DL |
85 | } |
86 | ||
8defc5be DL |
87 | static inline time_t monotime_to_realtime(const struct timeval *mono, |
88 | struct timeval *realout) | |
89 | { | |
90 | struct timeval delta, real; | |
91 | ||
92 | monotime_since(mono, &delta); | |
93 | gettimeofday(&real, NULL); | |
94 | ||
95 | timersub(&real, &delta, &real); | |
96 | if (realout) | |
97 | *realout = real; | |
98 | return real.tv_sec; | |
99 | } | |
100 | ||
c9049b92 MS |
101 | /* Char buffer size for time-to-string api */ |
102 | #define MONOTIME_STRLEN 32 | |
103 | ||
104 | static inline char *time_to_string(time_t ts, char *buf) | |
87454e6b CS |
105 | { |
106 | struct timeval tv; | |
107 | time_t tbuf; | |
108 | ||
109 | monotime(&tv); | |
110 | tbuf = time(NULL) - (tv.tv_sec - ts); | |
111 | ||
c9049b92 | 112 | return ctime_r(&tbuf, buf); |
87454e6b CS |
113 | } |
114 | ||
5e244469 RW |
115 | #ifdef __cplusplus |
116 | } | |
117 | #endif | |
118 | ||
7790a2d6 | 119 | #endif /* _FRR_MONOTIME_H */ |