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 /** \brief compare two 'struct timeval' and return the difference in seconds */
37 #define TIMEVAL_DIFF_SEC(tv_new, tv_old) \
38  (uint64_t)((((uint64_t)(tv_new).tv_sec * 1000000 + (tv_new).tv_usec) - \
39  ((uint64_t)(tv_old).tv_sec * 1000000 + (tv_old).tv_usec)) / \
40  1000000)
41 
42 /** \brief compare two 'struct timeval' and return if the first is earlier than the second */
43 #define TIMEVAL_EARLIER(tv_first, tv_second) \
44  (((tv_first).tv_sec < (tv_second).tv_sec) || \
45  ((tv_first).tv_sec == (tv_second).tv_sec && (tv_first).tv_usec < (tv_second).tv_usec))
46 
47 #ifdef UNITTESTS
48 void TimeSet(struct timeval *);
49 void TimeSetToCurrentTime(void);
50 void TimeSetIncrementTime(uint32_t);
51 #endif
52 
53 bool TimeModeIsReady(void);
54 void TimeModeSetLive(void);
55 void TimeModeSetOffline (void);
56 bool TimeModeIsLive(void);
57 
58 struct tm *SCLocalTime(time_t timep, struct tm *result);
59 void CreateTimeString(const struct timeval *ts, char *str, size_t size);
60 void CreateIsoTimeString(const struct timeval *ts, char *str, size_t size);
61 void CreateUtcIsoTimeString(const struct timeval *ts, char *str, size_t size);
62 void CreateFormattedTimeString(const struct tm *t, const char * fmt, char *str, size_t size);
63 time_t SCMkTimeUtc(struct tm *tp);
64 int SCStringPatternToTime(char *string, const char **patterns,
65  int num_patterns, struct tm *time);
66 int SCTimeToStringPattern (time_t epoch, const char *pattern, char *str,
67  size_t size);
68 uint64_t SCParseTimeSizeString (const char *str);
69 uint64_t SCGetSecondsUntil (const char *str, time_t epoch);
70 uint64_t SCTimespecAsEpochMillis(const struct timespec *ts);
71 
72 #endif /* __UTIL_TIME_H__ */
73 
TimeSetToCurrentTime
void TimeSetToCurrentTime(void)
set the time to "gettimeofday" meant for testing
Definition: util-time.c:142
ts
uint64_t ts
Definition: source-erf-file.c:54
TimeSet
void TimeSet(struct timeval *)
Definition: util-time.c:123
CreateTimeString
void CreateTimeString(const struct timeval *ts, char *str, size_t size)
Definition: util-time.c:275
CreateFormattedTimeString
void CreateFormattedTimeString(const struct tm *t, const char *fmt, char *str, size_t size)
Definition: util-time.c:249
TimeModeSetLive
void TimeModeSetLive(void)
Definition: util-time.c:97
CreateUtcIsoTimeString
void CreateUtcIsoTimeString(const struct timeval *ts, char *str, size_t size)
Definition: util-time.c:233
TimeSetByThread
void TimeSetByThread(const int thread_id, const struct timeval *tv)
Definition: util-time.c:114
SCMkTimeUtc
time_t SCMkTimeUtc(struct tm *tp)
Convert broken-down time to seconds since Unix epoch.
Definition: util-time.c:448
SCTimeToStringPattern
int SCTimeToStringPattern(time_t epoch, const char *pattern, char *str, size_t size)
Convert epoch time to string pattern.
Definition: util-time.c:547
TimeInit
void TimeInit(void)
Definition: util-time.c:77
TimeGet
void TimeGet(struct timeval *)
Definition: util-time.c:153
TimeModeIsLive
bool TimeModeIsLive(void)
Definition: util-time.c:109
SCTimespecAsEpochMillis
uint64_t SCTimespecAsEpochMillis(const struct timespec *ts)
Definition: util-time.c:645
TimeModeIsReady
bool TimeModeIsReady(void)
Definition: util-time.c:90
TimeModeSetOffline
void TimeModeSetOffline(void)
Definition: util-time.c:103
TimeDeinit
void TimeDeinit(void)
Definition: util-time.c:85
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
CreateIsoTimeString
void CreateIsoTimeString(const struct timeval *ts, char *str, size_t size)
Definition: util-time.c:213
str
#define str(s)
Definition: suricata-common.h:273
SCParseTimeSizeString
uint64_t SCParseTimeSizeString(const char *str)
Parse string containing time size (1m, 1h, etc).
Definition: util-time.c:576
SCGetSecondsUntil
uint64_t SCGetSecondsUntil(const char *str, time_t epoch)
Get seconds until a time unit changes.
Definition: util-time.c:627
SCLocalTime
struct tm * SCLocalTime(time_t timep, struct tm *result)
Definition: util-time.c:270
TimeSetIncrementTime
void TimeSetIncrementTime(uint32_t)
increment the time in the engine
Definition: util-time.c:182
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:491