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 74