56 #ifndef _POSIX_C_SOURCE
57 #define _POSIX_C_SOURCE 200809L
68 static struct timeval current_time = { 0, 0 };
72 static bool live_time_tracking =
true;
74 struct tm *
SCLocalTime(time_t timep,
struct tm *result);
75 struct tm *
SCUtcTime(time_t timep,
struct tm *result);
92 if (live_time_tracking)
99 live_time_tracking =
true;
105 live_time_tracking =
false;
111 return live_time_tracking;
116 if (live_time_tracking)
125 if (live_time_tracking)
132 current_time.tv_sec =
tv->tv_sec;
133 current_time.tv_usec =
tv->tv_usec;
135 SCLogDebug(
"time set to %" PRIuMAX
" sec, %" PRIuMAX
" usec",
136 (uintmax_t)current_time.tv_sec, (uintmax_t)current_time.tv_usec);
145 memset(&
tv, 0x00,
sizeof(
tv));
147 gettimeofday(&
tv, NULL);
158 if (live_time_tracking) {
159 gettimeofday(
tv, NULL);
164 tv->tv_sec = current_time.tv_sec;
165 tv->tv_usec = current_time.tv_usec;
175 SCLogDebug(
"time we got is %" PRIuMAX
" sec, %" PRIuMAX
" usec",
176 (uintmax_t)
tv->tv_sec, (uintmax_t)
tv->tv_usec);
185 memset(&
tv, 0x00,
sizeof(
tv));
199 static inline void WinStrftime(
const struct timeval *
ts,
const struct tm *t,
char *
str,
size_t size)
201 char time_fmt[64] = { 0 };
203 const long int tzdiff = -_timezone;
204 const int h = abs(_timezone) / 3600 + _daylight;
205 const int m = (abs(_timezone) % 3600) / 60;
206 snprintf(tz,
sizeof(tz),
"%c%02d%02d", tzdiff < 0 ?
'-' :
'+', h,
m);
207 strftime(time_fmt,
sizeof(time_fmt),
"%Y-%m-%dT%H:%M:%S.%%06u", t);
208 snprintf(
str, size, time_fmt,
ts->tv_usec);
215 time_t time =
ts->tv_sec;
217 memset(&local_tm, 0,
sizeof(local_tm));
218 struct tm *t = (
struct tm*)
SCLocalTime(time, &local_tm);
222 WinStrftime(
ts, t,
str, size);
224 char time_fmt[64] = { 0 };
225 strftime(time_fmt,
sizeof(time_fmt),
"%Y-%m-%dT%H:%M:%S.%%06u%z", t);
226 snprintf(
str, size, time_fmt,
ts->tv_usec);
229 snprintf(
str, size,
"ts-error");
235 time_t time =
ts->tv_sec;
237 memset(&local_tm, 0,
sizeof(local_tm));
238 struct tm *t = (
struct tm*)
SCUtcTime(time, &local_tm);
239 char time_fmt[64] = { 0 };
242 strftime(time_fmt,
sizeof(time_fmt),
"%Y-%m-%dT%H:%M:%S", t);
243 snprintf(
str, size, time_fmt,
ts->tv_usec);
245 snprintf(
str, size,
"ts-error");
251 if (
likely(t != NULL && fmt != NULL &&
str != NULL)) {
252 strftime(
str, size, fmt, t);
254 snprintf(
str, size,
"ts-error");
260 return gmtime_r(&timep, result);
272 return localtime_r(&timep, result);
277 time_t time =
ts->tv_sec;
279 struct tm *t = (
struct tm*)
SCLocalTime(time, &local_tm);
282 snprintf(
str, size,
"%02d/%02d/%02d-%02d:%02d:%02d.%06u",
283 t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour,
284 t->tm_min, t->tm_sec, (uint32_t)
ts->tv_usec);
286 snprintf(
str, size,
"ts-error");
298 #define MAX_LOCAL_TIME_STRING 32
300 static thread_local
int mru_time_slot;
301 static thread_local time_t last_local_time[2];
302 static thread_local
short int cached_local_time_len[2];
303 static thread_local
char cached_local_time[2][MAX_LOCAL_TIME_STRING];
307 static thread_local
int mru_tm_slot;
308 static thread_local time_t cached_minute_start[2];
309 static thread_local
struct tm cached_local_tm[2];
333 struct tm *
SCLocalTime(time_t timep,
struct tm *result)
337 int mru = mru_tm_slot;
339 int mru_seconds = timep - cached_minute_start[mru];
340 int lru_seconds = timep - cached_minute_start[lru];
342 if (cached_minute_start[mru]==0 && cached_minute_start[lru]==0) {
343 localtime_r(&timep, &cached_local_tm[lru]);
345 new_seconds = cached_local_tm[lru].tm_sec;
346 cached_minute_start[lru] = timep - new_seconds;
349 }
else if (lru_seconds > 0 && (mru_seconds >= 0 && mru_seconds <= 59)) {
351 new_seconds = mru_seconds;
352 }
else if (mru_seconds > 0 && (lru_seconds >= 0 && lru_seconds <= 59)) {
354 new_seconds = lru_seconds;
359 if (localtime_r(&timep, &cached_local_tm[lru]) == NULL)
362 new_seconds = cached_local_tm[lru].tm_sec;
363 cached_minute_start[lru] = timep - new_seconds;
367 memcpy(result, &cached_local_tm[mru],
sizeof(
struct tm));
368 result->tm_sec = new_seconds;
374 static int UpdateCachedTime(
int n, time_t time)
377 struct tm *t = (
struct tm *)
SCLocalTime(time, &local_tm);
378 int cached_len = snprintf(cached_local_time[n], MAX_LOCAL_TIME_STRING,
379 "%02d/%02d/%02d-%02d:%02d:",
380 t->tm_mon + 1, t->tm_mday, t->tm_year + 1900,
381 t->tm_hour, t->tm_min);
382 cached_local_time_len[n] = cached_len;
384 last_local_time[n] = time - t->tm_sec;
398 time_t time =
ts->tv_sec;
403 int mru = mru_time_slot;
405 int mru_seconds = time - last_local_time[mru];
406 int lru_seconds = time - last_local_time[lru];
407 if (last_local_time[mru]==0 && last_local_time[lru]==0) {
409 UpdateCachedTime(mru, time);
410 seconds = UpdateCachedTime(lru, time);
411 }
else if (mru_seconds >= 0 && mru_seconds <= 59) {
413 seconds = mru_seconds;
414 }
else if (lru_seconds >= 0 && lru_seconds <= 59) {
416 seconds = lru_seconds;
421 seconds = UpdateCachedTime(lru, time);
426 char *cached_str = cached_local_time[mru_time_slot];
427 int cached_len = cached_local_time_len[mru_time_slot];
428 if (cached_len >= (
int)size)
430 memcpy(
str, cached_str, cached_len);
431 snprintf(
str + cached_len, size - cached_len,
433 seconds, (uint32_t)
ts->tv_usec);
452 #define MONTHSPERYEAR 12
454 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
457 result = (year - 1970) * 365 + mdays[tp->tm_mon %
MONTHSPERYEAR];
458 result += (year - 1968) / 4;
459 result -= (year - 1900) / 100;
460 result += (year - 1600) / 400;
461 if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0) &&
464 result += tp->tm_mday - 1;
466 result += tp->tm_hour;
468 result += tp->tm_min;
470 result += tp->tm_sec;
473 result -= tp->tm_gmtoff;
498 for (i = 0; i < num_patterns; i++)
500 if (patterns[i] == NULL)
503 tp->tm_hour = tp->tm_min = tp->tm_sec = 0;
504 tp->tm_year = tp->tm_mon = tp->tm_mday = tp->tm_wday = INT_MIN;
510 result =
strptime(
string, patterns[i], tp);
512 if (result && *result ==
'\0')
517 if (result == NULL || *result !=
'\0')
521 if (tp->tm_year == INT_MIN && tp->tm_mon == INT_MIN &&
522 tp->tm_mday == INT_MIN)
527 if (tp->tm_year != INT_MIN && tp->tm_mon != INT_MIN &&
550 memset(&tm, 0,
sizeof(tm));
551 struct tm *tp = (
struct tm *)
SCLocalTime(epoch, &tm);
552 char buffer[PATH_MAX] = { 0 };
558 int r = strftime(buffer,
sizeof(buffer), pattern, tp);
579 uint64_t modifier = 1;
580 char last =
str[strlen(
str)-1];
599 modifier = 60 * 60 * 24;
603 modifier = 60 * 60 * 24 * 7;
611 size = strtoumax(
str, NULL, 10);
616 return (size * modifier);
629 uint64_t seconds = 0;
631 memset(&tm, 0,
sizeof(tm));
632 struct tm *tp = (
struct tm *)
SCLocalTime(epoch, &tm);
634 if (strcmp(
str,
"minute") == 0)
635 seconds = 60 - tp->tm_sec;
636 else if (strcmp(
str,
"hour") == 0)
637 seconds = (60 * (60 - tp->tm_min)) + (60 - tp->tm_sec);
638 else if (strcmp(
str,
"day") == 0)
639 seconds = (3600 * (24 - tp->tm_hour)) + (60 * (60 - tp->tm_min)) +
647 return ts->tv_sec * 1000L +
ts->tv_nsec / 1000000L;