]>
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
29 struct printfrr_eargs
;
31 #ifndef TIMESPEC_TO_TIMEVAL
32 /* should be in sys/time.h on BSD & Linux libcs */
33 #define TIMESPEC_TO_TIMEVAL(tv, ts) \
35 (tv)->tv_sec = (ts)->tv_sec; \
36 (tv)->tv_usec = (ts)->tv_nsec / 1000; \
39 #ifndef TIMEVAL_TO_TIMESPEC
40 /* should be in sys/time.h on BSD & Linux libcs */
41 #define TIMEVAL_TO_TIMESPEC(tv, ts) \
43 (ts)->tv_sec = (tv)->tv_sec; \
44 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
48 /* Linux/glibc is sadly missing these timespec helpers */
50 #define timespecadd(tsp, usp, vsp) \
52 (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
53 (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
54 if ((vsp)->tv_nsec >= 1000000000L) { \
56 (vsp)->tv_nsec -= 1000000000L; \
62 #define timespecsub(tsp, usp, vsp) \
64 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
65 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
66 if ((vsp)->tv_nsec < 0) { \
68 (vsp)->tv_nsec += 1000000000L; \
73 static inline time_t monotime(struct timeval
*tvo
)
77 clock_gettime(CLOCK_MONOTONIC
, &ts
);
79 TIMESPEC_TO_TIMEVAL(tvo
, &ts
);
84 #define ONE_DAY_SECOND 60*60*24
85 #define ONE_WEEK_SECOND ONE_DAY_SECOND*7
86 #define ONE_YEAR_SECOND ONE_DAY_SECOND*365
88 /* the following two return microseconds, not time_t!
90 * also, they're negative forms of each other, but having both makes the
93 static inline int64_t monotime_since(const struct timeval
*ref
,
98 timersub(&tv
, ref
, &tv
);
101 return (int64_t)tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
104 static inline int64_t monotime_until(const struct timeval
*ref
,
109 timersub(ref
, &tv
, &tv
);
112 return (int64_t)tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
115 static inline time_t monotime_to_realtime(const struct timeval
*mono
,
116 struct timeval
*realout
)
118 struct timeval delta
, real
;
120 monotime_since(mono
, &delta
);
121 gettimeofday(&real
, NULL
);
123 timersub(&real
, &delta
, &real
);
129 /* Char buffer size for time-to-string api */
130 #define MONOTIME_STRLEN 32
132 static inline char *time_to_string(time_t ts
, char *buf
)
138 tbuf
= time(NULL
) - (tv
.tv_sec
- ts
);
140 return ctime_r(&tbuf
, buf
);
143 /* Convert interval to human-friendly string, used in cli output e.g. */
144 static inline const char *frrtime_to_interval(time_t t
, char *buf
,
151 if (t
< ONE_DAY_SECOND
)
152 snprintf(buf
, buflen
, "%02d:%02d:%02d", tm
.tm_hour
, tm
.tm_min
,
154 else if (t
< ONE_WEEK_SECOND
)
155 snprintf(buf
, buflen
, "%dd%02dh%02dm", tm
.tm_yday
, tm
.tm_hour
,
158 snprintf(buf
, buflen
, "%02dw%dd%02dh", tm
.tm_yday
/ 7,
159 tm
.tm_yday
- ((tm
.tm_yday
/ 7) * 7), tm
.tm_hour
);
164 /* n/a - input was seconds precision, don't print any fractional */
165 TIMEFMT_SECONDS
= (1 << 0),
166 /* caller is directly invoking printfrr_time and has pre-specified
167 * I/Iu/Is/M/Mu/Ms/R/Ru/Rs (for printing timers)
169 TIMEFMT_PRESELECT
= (1 << 1),
170 /* don't print any output - this is needed for invoking printfrr_time
171 * from another printfrr extensions to skip over flag characters
173 TIMEFMT_SKIP
= (1 << 2),
174 /* use spaces in appropriate places */
175 TIMEFMT_SPACE
= (1 << 3),
177 /* input interpretations: */
178 TIMEFMT_REALTIME
= (1 << 8),
179 TIMEFMT_MONOTONIC
= (1 << 9),
180 TIMEFMT_SINCE
= (1 << 10),
181 TIMEFMT_UNTIL
= (1 << 11),
183 TIMEFMT_ABSOLUTE
= TIMEFMT_REALTIME
| TIMEFMT_MONOTONIC
,
184 TIMEFMT_ANCHORS
= TIMEFMT_SINCE
| TIMEFMT_UNTIL
,
186 /* calendaric formats: */
187 TIMEFMT_ISO8601
= (1 << 16),
189 /* interval formats: */
190 /* 't' - use [t]raditional 3-block format */
191 TIMEFMT_BASIC
= (1 << 24),
192 /* 'm' - select mm:ss */
193 TIMEFMT_MMSS
= (1 << 25),
194 /* 'h' - select hh:mm:ss */
195 TIMEFMT_HHMMSS
= (1 << 26),
196 /* 'd' - print as decimal number of seconds */
197 TIMEFMT_DECIMAL
= (1 << 27),
198 /* 'mx'/'hx' - replace zero value with "--:--" or "--:--:--" */
199 TIMEFMT_DASHES
= (1 << 31),
201 /* helpers for reference */
202 TIMEFMT_TIMER_DEADLINE
=
203 TIMEFMT_PRESELECT
| TIMEFMT_MONOTONIC
| TIMEFMT_UNTIL
,
204 TIMEFMT_TIMER_INTERVAL
= TIMEFMT_PRESELECT
,
207 extern ssize_t
printfrr_time(struct fbuf
*buf
, struct printfrr_eargs
*ea
,
208 const struct timespec
*ts
, unsigned int flags
);
214 #endif /* _FRR_MONOTIME_H */