]>
Commit | Line | Data |
---|---|---|
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 | ||
24 | #ifndef TIMESPEC_TO_TIMEVAL | |
25 | /* should be in sys/time.h on BSD & Linux libcs */ | |
26 | #define TIMESPEC_TO_TIMEVAL(tv, ts) \ | |
27 | do { \ | |
28 | (tv)->tv_sec = (ts)->tv_sec; \ | |
29 | (tv)->tv_usec = (ts)->tv_nsec / 1000; \ | |
30 | } while (0) | |
31 | #endif | |
32 | #ifndef TIMEVAL_TO_TIMESPEC | |
33 | /* should be in sys/time.h on BSD & Linux libcs */ | |
34 | #define TIMEVAL_TO_TIMESPEC(tv, ts) \ | |
35 | do { \ | |
36 | (ts)->tv_sec = (tv)->tv_sec; \ | |
37 | (ts)->tv_nsec = (tv)->tv_usec * 1000; \ | |
38 | } while (0) | |
39 | #endif | |
40 | ||
41 | static inline time_t monotime(struct timeval *tvo) | |
42 | { | |
43 | struct timespec ts; | |
44 | ||
45 | clock_gettime(CLOCK_MONOTONIC, &ts); | |
46 | if (tvo) { | |
47 | TIMESPEC_TO_TIMEVAL(tvo, &ts); | |
48 | } | |
49 | return ts.tv_sec; | |
50 | } | |
51 | ||
52 | #define ONE_DAY_SECOND 60*60*24 | |
53 | #define ONE_WEEK_SECOND ONE_DAY_SECOND*7 | |
54 | #define ONE_YEAR_SECOND ONE_DAY_SECOND*365 | |
55 | ||
56 | /* the following two return microseconds, not time_t! | |
57 | * | |
58 | * also, they're negative forms of each other, but having both makes the | |
59 | * code more readable | |
60 | */ | |
61 | static inline int64_t monotime_since(const struct timeval *ref, | |
62 | struct timeval *out) | |
63 | { | |
64 | struct timeval tv; | |
65 | monotime(&tv); | |
66 | timersub(&tv, ref, &tv); | |
67 | if (out) | |
68 | *out = tv; | |
69 | return (int64_t)tv.tv_sec * 1000000LL + tv.tv_usec; | |
70 | } | |
71 | ||
72 | static inline int64_t monotime_until(const struct timeval *ref, | |
73 | struct timeval *out) | |
74 | { | |
75 | struct timeval tv; | |
76 | monotime(&tv); | |
77 | timersub(ref, &tv, &tv); | |
78 | if (out) | |
79 | *out = tv; | |
80 | return (int64_t)tv.tv_sec * 1000000LL + tv.tv_usec; | |
81 | } | |
82 | ||
83 | static inline char *time_to_string(time_t ts) | |
84 | { | |
85 | struct timeval tv; | |
86 | time_t tbuf; | |
87 | ||
88 | monotime(&tv); | |
89 | tbuf = time(NULL) - (tv.tv_sec - ts); | |
90 | ||
91 | return ctime(&tbuf); | |
92 | } | |
93 | ||
94 | #endif /* _FRR_MONOTIME_H */ |