]> git.proxmox.com Git - mirror_frr.git/blame - lib/monotime.h
zebra: Allow ns delete to happen after under/over flow checks
[mirror_frr.git] / lib / monotime.h
CommitLineData
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
24#ifndef TIMESPEC_TO_TIMEVAL
25/* should be in sys/time.h on BSD & Linux libcs */
d62a17ae 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)
7790a2d6 31#endif
39cea822
DL
32#ifndef TIMEVAL_TO_TIMESPEC
33/* should be in sys/time.h on BSD & Linux libcs */
d62a17ae 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)
39cea822 39#endif
7790a2d6
DL
40
41static inline time_t monotime(struct timeval *tvo)
42{
d62a17ae 43 struct timespec ts;
7790a2d6 44
d62a17ae 45 clock_gettime(CLOCK_MONOTONIC, &ts);
46 if (tvo) {
47 TIMESPEC_TO_TIMEVAL(tvo, &ts);
48 }
49 return ts.tv_sec;
7790a2d6
DL
50}
51
99a6a31e
DS
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
7790a2d6
DL
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 */
61static inline int64_t monotime_since(const struct timeval *ref,
d62a17ae 62 struct timeval *out)
7790a2d6 63{
d62a17ae 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;
7790a2d6
DL
70}
71
72static inline int64_t monotime_until(const struct timeval *ref,
d62a17ae 73 struct timeval *out)
7790a2d6 74{
d62a17ae 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;
7790a2d6
DL
81}
82
87454e6b
CS
83static 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
7790a2d6 94#endif /* _FRR_MONOTIME_H */