suricata
util-time.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2013 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  */
23 
24 #ifndef __UTIL_TIME_H__
25 #define __UTIL_TIME_H__
26 
27 void TimeInit(void);
28 void TimeDeinit(void);
29 
30 void TimeSetByThread(const int thread_id, const struct timeval *tv);
31 void TimeGet(struct timeval *);
32 
33 /** \brief intialize a 'struct timespec' from a 'struct timeval'. */
34 #define FROM_TIMEVAL(timev) { .tv_sec = (timev).tv_sec, .tv_nsec = (timev).tv_usec * 1000 }
35 
36 static inline struct timeval TimevalWithSeconds(const struct timeval *ts, const time_t sec_add)
37 {
38 #ifdef timeradd
39  struct timeval add = { .tv_sec = sec_add, .tv_usec = 0 };
40  struct timeval result;
41  timeradd(ts, &add, &result);
42  return result;
43 #else
44  const time_t sec = ts->tv_sec + sec_add;
45  struct timeval result = { .tv_sec = sec, .tv_usec = ts->tv_usec };
46  return result;
47 #endif
48 }
49 
50 /** \brief compare two 'struct timeval' and return if the first is earlier than the second */
51 static inline bool TimevalEarlier(struct timeval *first, struct timeval *second)
52 {
53  /* from man timercmp on Linux: "Some systems (but not Linux/glibc), have a broken timercmp()
54  * implementation, in which CMP of >=, <=, and == do not work; portable applications can instead
55  * use ... !timercmp(..., >) */
56  return !timercmp(first, second, >);
57 }
58 
59 #ifndef timeradd
60 #define timeradd(a, b, r) \
61  do { \
62  (r)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
63  (r)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
64  if ((r)->tv_usec >= 1000000) { \
65  (r)->tv_sec++; \
66  (r)->tv_usec -= 1000000; \
67  } \
68  } while (0)
69 #endif
70 
71 #ifdef UNITTESTS
72 void TimeSet(struct timeval *);
73 void TimeSetToCurrentTime(void);
74 void TimeSetIncrementTime(uint32_t);
75 #endif
76 
77 bool TimeModeIsReady(void);
78 void TimeModeSetLive(void);
79 void TimeModeSetOffline (void);
80 bool TimeModeIsLive(void);
81 
82 struct tm *SCLocalTime(time_t timep, struct tm *result);
83 void CreateTimeString(const struct timeval *ts, char *str, size_t size);
84 void CreateIsoTimeString(const struct timeval *ts, char *str, size_t size);
85 void CreateUtcIsoTimeString(const struct timeval *ts, char *str, size_t size);
86 void CreateFormattedTimeString(const struct tm *t, const char * fmt, char *str, size_t size);
87 time_t SCMkTimeUtc(struct tm *tp);
88 int SCStringPatternToTime(char *string, const char **patterns,
89  int num_patterns, struct tm *time);
90 int SCTimeToStringPattern (time_t epoch, const char *pattern, char *str,
91  size_t size);
92 uint64_t SCParseTimeSizeString (const char *str);
93 uint64_t SCGetSecondsUntil (const char *str, time_t epoch);
94 uint64_t SCTimespecAsEpochMillis(const struct timespec *ts);
95 uint64_t TimeDifferenceMicros(struct timeval t0, struct timeval t1);
96 
97 #endif /* __UTIL_TIME_H__ */
98 
TimeSetToCurrentTime
void TimeSetToCurrentTime(void)
set the time to "gettimeofday" meant for testing
Definition: util-time.c:144
ts
uint64_t ts
Definition: source-erf-file.c:55
TimeSet
void TimeSet(struct timeval *)
Definition: util-time.c:125
CreateTimeString
void CreateTimeString(const struct timeval *ts, char *str, size_t size)
Definition: util-time.c:278
CreateFormattedTimeString
void CreateFormattedTimeString(const struct tm *t, const char *fmt, char *str, size_t size)
Definition: util-time.c:252
TimeModeSetLive
void TimeModeSetLive(void)
Definition: util-time.c:99
CreateUtcIsoTimeString
void CreateUtcIsoTimeString(const struct timeval *ts, char *str, size_t size)
Definition: util-time.c:236
TimeSetByThread
void TimeSetByThread(const int thread_id, const struct timeval *tv)
Definition: util-time.c:116
SCMkTimeUtc
time_t SCMkTimeUtc(struct tm *tp)
Convert broken-down time to seconds since Unix epoch.
Definition: util-time.c:451
SCTimeToStringPattern
int SCTimeToStringPattern(time_t epoch, const char *pattern, char *str, size_t size)
Convert epoch time to string pattern.
Definition: util-time.c:550
TimeInit
void TimeInit(void)
Definition: util-time.c:79
TimeGet
void TimeGet(struct timeval *)
Definition: util-time.c:155
TimeModeIsLive
bool TimeModeIsLive(void)
Definition: util-time.c:111
SCTimespecAsEpochMillis
uint64_t SCTimespecAsEpochMillis(const struct timespec *ts)
Definition: util-time.c:648
TimeModeIsReady
bool TimeModeIsReady(void)
Definition: util-time.c:92
TimeModeSetOffline
void TimeModeSetOffline(void)
Definition: util-time.c:105
TimeDeinit
void TimeDeinit(void)
Definition: util-time.c:87
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:31
CreateIsoTimeString
void CreateIsoTimeString(const struct timeval *ts, char *str, size_t size)
Definition: util-time.c:215
str
#define str(s)
Definition: suricata-common.h:272
TimeDifferenceMicros
uint64_t TimeDifferenceMicros(struct timeval t0, struct timeval t1)
Definition: util-time.c:653
SCParseTimeSizeString
uint64_t SCParseTimeSizeString(const char *str)
Parse string containing time size (1m, 1h, etc).
Definition: util-time.c:579
timeradd
#define timeradd(a, b, r)
Definition: util-time.h:60
SCGetSecondsUntil
uint64_t SCGetSecondsUntil(const char *str, time_t epoch)
Get seconds until a time unit changes.
Definition: util-time.c:630
SCLocalTime
struct tm * SCLocalTime(time_t timep, struct tm *result)
Definition: util-time.c:273
TimeSetIncrementTime
void TimeSetIncrementTime(uint32_t)
increment the time in the engine
Definition: util-time.c:184
SCStringPatternToTime
int SCStringPatternToTime(char *string, const char **patterns, int num_patterns, struct tm *time)
Parse a date string based on specified patterns.
Definition: util-time.c:494