| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #ifndef _FRR_MONOTIME_H |
| #define _FRR_MONOTIME_H |
| |
| #include <stdint.h> |
| #include <time.h> |
| #include <sys/time.h> |
| |
| #ifndef TIMESPEC_TO_TIMEVAL |
| |
| #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 |
| |
| #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 |
| |
| |
| |
| |
| |
| |
| 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); |
| } |
| |
| #endif |