60 static struct timeval current_time = { 0, 0 };
64 static char live =
TRUE;
66 struct tm *
SCLocalTime(time_t timep,
struct tm *result);
67 struct tm *
SCUtcTime(time_t timep,
struct tm *result);
117 current_time.tv_sec = tv->tv_sec;
118 current_time.tv_usec = tv->tv_usec;
120 SCLogDebug(
"time set to %" PRIuMAX
" sec, %" PRIuMAX
" usec",
121 (uintmax_t)current_time.tv_sec, (uintmax_t)current_time.tv_usec);
130 memset(&tv, 0x00,
sizeof(tv));
132 gettimeofday(&tv, NULL);
144 gettimeofday(tv, NULL);
149 tv->tv_sec = current_time.tv_sec;
150 tv->tv_usec = current_time.tv_usec;
160 SCLogDebug(
"time we got is %" PRIuMAX
" sec, %" PRIuMAX
" usec",
161 (uintmax_t)tv->tv_sec, (uintmax_t)tv->tv_usec);
170 memset(&tv, 0x00,
sizeof(tv));
181 time_t time = ts->tv_sec;
183 memset(&local_tm, 0,
sizeof(local_tm));
184 struct tm *t = (
struct tm*)
SCLocalTime(time, &local_tm);
185 char time_fmt[64] = { 0 };
188 strftime(time_fmt,
sizeof(time_fmt),
"%Y-%m-%dT%H:%M:%S.%%06u%z", t);
189 snprintf(str, size, time_fmt, ts->tv_usec);
191 snprintf(str, size,
"ts-error");
197 time_t time = ts->tv_sec;
199 memset(&local_tm, 0,
sizeof(local_tm));
200 struct tm *t = (
struct tm*)
SCUtcTime(time, &local_tm);
201 char time_fmt[64] = { 0 };
204 strftime(time_fmt,
sizeof(time_fmt),
"%Y-%m-%dT%H:%M:%S", t);
205 snprintf(str, size, time_fmt, ts->tv_usec);
207 snprintf(str, size,
"ts-error");
213 if (
likely(t != NULL && fmt != NULL && str != NULL)) {
214 strftime(str, size, fmt, t);
216 snprintf(str, size,
"ts-error");
222 return gmtime_r(&timep, result);
234 return localtime_r(&timep, result);
239 time_t time = ts->tv_sec;
241 struct tm *t = (
struct tm*)
SCLocalTime(time, &local_tm);
244 snprintf(str, size,
"%02d/%02d/%02d-%02d:%02d:%02d.%06u",
245 t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour,
246 t->tm_min, t->tm_sec, (uint32_t) ts->tv_usec);
248 snprintf(str, size,
"ts-error");
260 #define MAX_LOCAL_TIME_STRING 32 262 static __thread
int mru_time_slot;
263 static __thread time_t last_local_time[2];
264 static __thread
short int cached_local_time_len[2];
265 static __thread
char cached_local_time[2][MAX_LOCAL_TIME_STRING];
269 static __thread
int mru_tm_slot;
270 static __thread time_t cached_minute_start[2];
271 static __thread
struct tm cached_local_tm[2];
295 struct tm *
SCLocalTime(time_t timep,
struct tm *result)
299 int mru = mru_tm_slot;
301 int mru_seconds = timep - cached_minute_start[mru];
302 int lru_seconds = timep - cached_minute_start[lru];
304 if (cached_minute_start[mru]==0 && cached_minute_start[lru]==0) {
305 localtime_r(&timep, &cached_local_tm[lru]);
307 new_seconds = cached_local_tm[lru].tm_sec;
308 cached_minute_start[lru] = timep - new_seconds;
311 }
else if (lru_seconds > 0 && (mru_seconds >= 0 && mru_seconds <= 59)) {
313 new_seconds = mru_seconds;
314 }
else if (mru_seconds > 0 && (lru_seconds >= 0 && lru_seconds <= 59)) {
316 new_seconds = lru_seconds;
321 if (localtime_r(&timep, &cached_local_tm[lru]) == NULL)
324 new_seconds = cached_local_tm[lru].tm_sec;
325 cached_minute_start[lru] = timep - new_seconds;
329 memcpy(result, &cached_local_tm[mru],
sizeof(
struct tm));
330 result->tm_sec = new_seconds;
336 static int UpdateCachedTime(
int n, time_t time)
339 struct tm *t = (
struct tm *)
SCLocalTime(time, &local_tm);
340 int cached_len = snprintf(cached_local_time[n], MAX_LOCAL_TIME_STRING,
341 "%02d/%02d/%02d-%02d:%02d:",
342 t->tm_mon + 1, t->tm_mday, t->tm_year + 1900,
343 t->tm_hour, t->tm_min);
344 cached_local_time_len[n] = cached_len;
346 last_local_time[n] = time - t->tm_sec;
360 time_t time =
ts->tv_sec;
365 int mru = mru_time_slot;
367 int mru_seconds = time - last_local_time[mru];
368 int lru_seconds = time - last_local_time[lru];
369 if (last_local_time[mru]==0 && last_local_time[lru]==0) {
371 UpdateCachedTime(mru, time);
372 seconds = UpdateCachedTime(lru, time);
373 }
else if (mru_seconds >= 0 && mru_seconds <= 59) {
375 seconds = mru_seconds;
376 }
else if (lru_seconds >= 0 && lru_seconds <= 59) {
378 seconds = lru_seconds;
383 seconds = UpdateCachedTime(lru, time);
388 char *cached_str = cached_local_time[mru_time_slot];
389 int cached_len = cached_local_time_len[mru_time_slot];
390 if (cached_len >= (
int)size)
392 memcpy(
str, cached_str, cached_len);
393 snprintf(
str + cached_len, size - cached_len,
395 seconds, (uint32_t)
ts->tv_usec);
414 #define MONTHSPERYEAR 12 416 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
419 result = (year - 1970) * 365 + mdays[tp->tm_mon %
MONTHSPERYEAR];
420 result += (year - 1968) / 4;
421 result -= (year - 1900) / 100;
422 result += (year - 1600) / 400;
423 if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0) &&
426 result += tp->tm_mday - 1;
428 result += tp->tm_hour;
430 result += tp->tm_min;
432 result += tp->tm_sec;
435 result -= tp->tm_gmtoff;
460 for (i = 0; i < num_patterns; i++)
462 if (patterns[i] == NULL)
465 tp->tm_hour = tp->tm_min = tp->tm_sec = 0;
466 tp->tm_year = tp->tm_mon = tp->tm_mday = tp->tm_wday = INT_MIN;
472 result =
strptime(
string, patterns[i], tp);
474 if (result && *result ==
'\0')
479 if (result == NULL || *result !=
'\0')
483 if (tp->tm_year == INT_MIN && tp->tm_mon == INT_MIN &&
484 tp->tm_mday == INT_MIN)
489 if (tp->tm_year != INT_MIN && tp->tm_mon != INT_MIN &&
512 memset(&tm, 0,
sizeof(tm));
513 struct tm *tp = (
struct tm *)
SCLocalTime(epoch, &tm);
514 char buffer[PATH_MAX] = { 0 };
520 int r = strftime(buffer,
sizeof(buffer), pattern, tp);
541 uint64_t modifier = 1;
542 char last = str[strlen(str)-1];
561 modifier = 60 * 60 * 24;
565 modifier = 60 * 60 * 24 * 7;
573 size = strtoumax(str, NULL, 10);
578 return (size * modifier);
591 uint64_t seconds = 0;
593 memset(&tm, 0,
sizeof(tm));
594 struct tm *tp = (
struct tm *)
SCLocalTime(epoch, &tm);
596 if (strcmp(str,
"minute") == 0)
597 seconds = 60 - tp->tm_sec;
598 else if (strcmp(str,
"hour") == 0)
599 seconds = (60 * (60 - tp->tm_min)) + (60 - tp->tm_sec);
600 else if (strcmp(str,
"day") == 0)
601 seconds = (3600 * (24 - tp->tm_hour)) + (60 * (60 - tp->tm_min)) +
609 return ts->tv_sec * 1000L + ts->tv_nsec / 1000000L;
void TimeSetToCurrentTime(void)
set the time to "gettimeofday" meant for testing
size_t strlcpy(char *dst, const char *src, size_t siz)
#define SCSpinInit(spin, spin_attr)
void TimeSetByThread(const int thread_id, const struct timeval *tv)
uint64_t SCGetSecondsUntil(const char *str, time_t epoch)
Get seconds until a time unit changes.
char * strptime(const char *__restrict, const char *__restrict, struct tm *__restrict)
void TimeModeSetLive(void)
struct tm * SCUtcTime(time_t timep, struct tm *result)
void TmThreadsSetThreadTimestamp(const int id, const struct timeval *ts)
void TimeModeSetOffline(void)
void CreateUtcIsoTimeString(const struct timeval *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.
time_t SCMkTimeUtc(struct tm *tp)
Convert broken-down time to seconds since Unix epoch.
void CreateIsoTimeString(const struct timeval *ts, char *str, size_t size)
struct tm * SCLocalTime(time_t timep, struct tm *result)
void TimeGet(struct timeval *tv)
int RunmodeIsUnittests(void)
uint64_t SCTimespecAsEpochMillis(const struct timespec *ts)
void TimeSet(struct timeval *tv)
void TmreadsGetMinimalTimestamp(struct timeval *ts)
void TimeSetIncrementTime(uint32_t tv_sec)
increment the time in the engine
void CreateFormattedTimeString(const struct tm *t, const char *fmt, char *str, size_t size)
int SCStringPatternToTime(char *string, const char **patterns, int num_patterns, struct tm *tp)
Parse a date string based on specified patterns.
#define SCSpinDestroy(spin)
#define SCSpinUnlock(spin)
uint64_t SCParseTimeSizeString(const char *str)
Parse string containing time size (1m, 1h, etc).
void CreateTimeString(const struct timeval *ts, char *str, size_t size)