Go to the documentation of this file.
24 #ifndef __UTIL_TIME_H__
25 #define __UTIL_TIME_H__
45 #define SCTIME_INIT(t) \
51 #define SCTIME_INITIALIZER \
54 .secs = 0, .usecs = 0 \
56 #define SCTIME_USECS(t) ((uint64_t)(t).usecs)
57 #define SCTIME_SECS(t) ((uint64_t)(t).secs)
58 #define SCTIME_MSECS(t) (SCTIME_SECS(t) * 1000 + SCTIME_USECS(t) / 1000)
59 #define SCTIME_ADD_SECS(ts, s) SCTIME_FROM_SECS((ts).secs + (s))
60 #define SCTIME_ADD_USECS(ts, us) SCTIME_FROM_USECS((ts).usecs + (us))
61 #define SCTIME_FROM_SECS(s) \
64 .secs = (s), .usecs = 0 \
66 #define SCTIME_FROM_USECS(us) \
69 .secs = 0, .usecs = (us) \
71 #define SCTIME_FROM_TIMEVAL(tv) \
74 .secs = (tv)->tv_sec, .usecs = (tv)->tv_usec \
76 #define SCTIME_FROM_TIMESPEC(ts) \
79 .secs = (ts)->tv_sec, .usecs = (ts)->tv_nsec * 1000 \
82 #define SCTIME_TO_TIMEVAL(tv, t) \
83 (tv)->tv_sec = SCTIME_SECS((t)); \
84 (tv)->tv_usec = SCTIME_USECS((t));
85 #define SCTIME_CMP(a, b, CMP) \
86 ((SCTIME_SECS(a) == SCTIME_SECS(b)) ? (SCTIME_USECS(a) CMP SCTIME_USECS(b)) \
87 : (SCTIME_SECS(a) CMP SCTIME_SECS(b)))
88 #define SCTIME_CMP_GTE(a, b) SCTIME_CMP((a), (b), >=)
89 #define SCTIME_CMP_GT(a, b) SCTIME_CMP((a), (b), >)
90 #define SCTIME_CMP_LT(a, b) SCTIME_CMP((a), (b), <)
91 #define SCTIME_CMP_LTE(a, b) SCTIME_CMP((a), (b), <=)
92 #define SCTIME_CMP_NEQ(a, b) SCTIME_CMP((a), (b), !=)
101 #define FROM_TIMEVAL(timev) { .tv_sec = (timev).tv_sec, .tv_nsec = (timev).tv_usec * 1000 }
104 static inline bool TimevalEarlier(
struct timeval *first,
struct timeval *second)
109 return !timercmp(first, second, >);
113 #define timeradd(a, b, r) \
115 (r)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
116 (r)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
117 if ((r)->tv_usec >= 1000000) { \
119 (r)->tv_usec -= 1000000; \
135 struct tm *
SCLocalTime(time_t timep,
struct tm *result);
142 int num_patterns,
struct tm *time);
void TimeSetToCurrentTime(void)
set the time to "gettimeofday" meant for testing
void CreateFormattedTimeString(const struct tm *t, const char *fmt, char *str, size_t size)
void TimeModeSetLive(void)
time_t SCMkTimeUtc(struct tm *tp)
Convert broken-down time to seconds since Unix epoch.
void CreateUtcIsoTimeString(const SCTime_t ts, char *str, size_t size)
int SCTimeToStringPattern(time_t epoch, const char *pattern, char *str, size_t size)
Convert epoch time to string pattern.
bool TimeModeIsLive(void)
uint64_t SCTimespecAsEpochMillis(const struct timespec *ts)
bool TimeModeIsReady(void)
void TimeModeSetOffline(void)
void CreateIsoTimeString(const SCTime_t ts, char *str, size_t size)
uint64_t TimeDifferenceMicros(struct timeval t0, struct timeval t1)
uint64_t SCParseTimeSizeString(const char *str)
Parse string containing time size (1m, 1h, etc).
void TimeSetByThread(const int thread_id, SCTime_t tv)
uint64_t SCGetSecondsUntil(const char *str, time_t epoch)
Get seconds until a time unit changes.
void CreateTimeString(const SCTime_t ts, char *str, size_t size)
struct tm * SCLocalTime(time_t timep, struct tm *result)
void TimeSetIncrementTime(uint32_t)
increment the time in the engine
int SCStringPatternToTime(char *string, const char **patterns, int num_patterns, struct tm *time)
Parse a date string based on specified patterns.