]>
git.proxmox.com Git - mirror_frr.git/blob - lib/monotime.h
2 * Copyright (c) 2017 David Lamparter, for NetDEF, Inc.
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.
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.
17 #ifndef _FRR_MONOTIME_H
18 #define _FRR_MONOTIME_H
24 #ifndef TIMESPEC_TO_TIMEVAL
25 /* should be in sys/time.h on BSD & Linux libcs */
26 #define TIMESPEC_TO_TIMEVAL(tv, ts) \
28 (tv)->tv_sec = (ts)->tv_sec; \
29 (tv)->tv_usec = (ts)->tv_nsec / 1000; \
32 #ifndef TIMEVAL_TO_TIMESPEC
33 /* should be in sys/time.h on BSD & Linux libcs */
34 #define TIMEVAL_TO_TIMESPEC(tv, ts) \
36 (ts)->tv_sec = (tv)->tv_sec; \
37 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
41 static inline time_t monotime(struct timeval
*tvo
)
45 clock_gettime(CLOCK_MONOTONIC
, &ts
);
47 TIMESPEC_TO_TIMEVAL(tvo
, &ts
);
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
56 /* the following two return microseconds, not time_t!
58 * also, they're negative forms of each other, but having both makes the
61 static inline int64_t monotime_since(const struct timeval
*ref
,
66 timersub(&tv
, ref
, &tv
);
69 return (int64_t)tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
72 static inline int64_t monotime_until(const struct timeval
*ref
,
77 timersub(ref
, &tv
, &tv
);
80 return (int64_t)tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
83 static inline char *time_to_string(time_t ts
)
89 tbuf
= time(NULL
) - (tv
.tv_sec
- ts
);
94 #endif /* _FRR_MONOTIME_H */