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