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 /*
28  * The SCTime_t member is broken up as
29  * seconds: 44
30  * useconds: 20
31  *
32  * Over 500000 years can be represented in 44 bits of seconds:
33  * 2^44/(365*24*60*60)
34  * 557855.560
35  * 1048576 microseconds can be represented in 20 bits:
36  * 2^20
37  * 1048576
38  */
39 
40 typedef struct {
41  uint64_t secs : 44;
42  uint64_t usecs : 20;
43 } SCTime_t;
44 
45 #define SCTIME_INIT(t) \
46  { \
47  (t).secs = 0; \
48  (t).usecs = 0; \
49  }
50 
51 #define SCTIME_INITIALIZER \
52  (SCTime_t) \
53  { \
54  .secs = 0, .usecs = 0 \
55  }
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) \
62  (SCTime_t) \
63  { \
64  .secs = (s), .usecs = 0 \
65  }
66 #define SCTIME_FROM_USECS(us) \
67  (SCTime_t) \
68  { \
69  .secs = 0, .usecs = (us) \
70  }
71 #define SCTIME_FROM_TIMEVAL(tv) \
72  (SCTime_t) \
73  { \
74  .secs = (tv)->tv_sec, .usecs = (tv)->tv_usec \
75  }
76 #define SCTIME_FROM_TIMESPEC(ts) \
77  (SCTime_t) \
78  { \
79  .secs = (ts)->tv_sec, .usecs = (ts)->tv_nsec * 1000 \
80  }
81 
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), !=)
93 
94 void TimeInit(void);
95 void TimeDeinit(void);
96 
97 void TimeSetByThread(const int thread_id, SCTime_t tv);
98 SCTime_t TimeGet(void);
99 
100 /** \brief initialize a 'struct timespec' from a 'struct timeval'. */
101 #define FROM_TIMEVAL(timev) { .tv_sec = (timev).tv_sec, .tv_nsec = (timev).tv_usec * 1000 }
102 
103 /** \brief compare two 'struct timeval' and return if the first is earlier than the second */
104 static inline bool TimevalEarlier(struct timeval *first, struct timeval *second)
105 {
106  /* from man timercmp on Linux: "Some systems (but not Linux/glibc), have a broken timercmp()
107  * implementation, in which CMP of >=, <=, and == do not work; portable applications can instead
108  * use ... !timercmp(..., >) */
109  return !timercmp(first, second, >);
110 }
111 
112 #ifndef timeradd
113 #define timeradd(a, b, r) \
114  do { \
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) { \
118  (r)->tv_sec++; \
119  (r)->tv_usec -= 1000000; \
120  } \
121  } while (0)
122 #endif
123 
124 #ifdef UNITTESTS
125 void TimeSet(SCTime_t);
126 void TimeSetToCurrentTime(void);
127 void TimeSetIncrementTime(uint32_t);
128 #endif
129 
130 bool TimeModeIsReady(void);
131 void TimeModeSetLive(void);
132 void TimeModeSetOffline (void);
133 bool TimeModeIsLive(void);
134 
135 struct tm *SCLocalTime(time_t timep, struct tm *result);
136 void CreateTimeString(const SCTime_t ts, char *str, size_t size);
137 void CreateIsoTimeString(const SCTime_t ts, char *str, size_t size);
138 void CreateUtcIsoTimeString(const SCTime_t ts, char *str, size_t size);
139 void CreateFormattedTimeString(const struct tm *t, const char * fmt, char *str, size_t size);
140 time_t SCMkTimeUtc(struct tm *tp);
141 int SCStringPatternToTime(char *string, const char **patterns,
142  int num_patterns, struct tm *time);
143 int SCTimeToStringPattern (time_t epoch, const char *pattern, char *str,
144  size_t size);
145 uint64_t SCParseTimeSizeString (const char *str);
146 uint64_t SCGetSecondsUntil (const char *str, time_t epoch);
147 uint64_t SCTimespecAsEpochMillis(const struct timespec *ts);
148 uint64_t TimeDifferenceMicros(struct timeval t0, struct timeval t1);
149 
150 #endif /* __UTIL_TIME_H__ */
151 
TimeSetToCurrentTime
void TimeSetToCurrentTime(void)
set the time to "gettimeofday" meant for testing
Definition: util-time.c:140
ts
uint64_t ts
Definition: source-erf-file.c:55
TimeSet
void TimeSet(SCTime_t)
Definition: util-time.c:125
CreateFormattedTimeString
void CreateFormattedTimeString(const struct tm *t, const char *fmt, char *str, size_t size)
Definition: util-time.c:246
TimeModeSetLive
void TimeModeSetLive(void)
Definition: util-time.c:99
SCMkTimeUtc
time_t SCMkTimeUtc(struct tm *tp)
Convert broken-down time to seconds since Unix epoch.
Definition: util-time.c:442
CreateUtcIsoTimeString
void CreateUtcIsoTimeString(const SCTime_t ts, char *str, size_t size)
Definition: util-time.c:230
SCTimeToStringPattern
int SCTimeToStringPattern(time_t epoch, const char *pattern, char *str, size_t size)
Convert epoch time to string pattern.
Definition: util-time.c:541
TimeInit
void TimeInit(void)
Definition: util-time.c:79
SCTime_t::secs
uint64_t secs
Definition: util-time.h:41
TimeModeIsLive
bool TimeModeIsLive(void)
Definition: util-time.c:111
SCTime_t::usecs
uint64_t usecs
Definition: util-time.h:42
SCTimespecAsEpochMillis
uint64_t SCTimespecAsEpochMillis(const struct timespec *ts)
Definition: util-time.c:639
SCTime_t
Definition: util-time.h:40
TimeGet
SCTime_t TimeGet(void)
Definition: util-time.c:152
TimeModeIsReady
bool TimeModeIsReady(void)
Definition: util-time.c:92
TimeModeSetOffline
void TimeModeSetOffline(void)
Definition: util-time.c:105
CreateIsoTimeString
void CreateIsoTimeString(const SCTime_t ts, char *str, size_t size)
Definition: util-time.c:209
TimeDeinit
void TimeDeinit(void)
Definition: util-time.c:87
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
str
#define str(s)
Definition: suricata-common.h:286
TimeDifferenceMicros
uint64_t TimeDifferenceMicros(struct timeval t0, struct timeval t1)
Definition: util-time.c:644
SCParseTimeSizeString
uint64_t SCParseTimeSizeString(const char *str)
Parse string containing time size (1m, 1h, etc).
Definition: util-time.c:570
TimeSetByThread
void TimeSetByThread(const int thread_id, SCTime_t tv)
Definition: util-time.c:116
SCGetSecondsUntil
uint64_t SCGetSecondsUntil(const char *str, time_t epoch)
Get seconds until a time unit changes.
Definition: util-time.c:621
CreateTimeString
void CreateTimeString(const SCTime_t ts, char *str, size_t size)
Definition: util-time.c:272
SCLocalTime
struct tm * SCLocalTime(time_t timep, struct tm *result)
Definition: util-time.c:267
TimeSetIncrementTime
void TimeSetIncrementTime(uint32_t)
increment the time in the engine
Definition: util-time.c:180
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:485