56 #ifndef _POSIX_C_SOURCE
57 #define _POSIX_C_SOURCE 200809L
70 static struct timeval current_time = { 0, 0 };
74 static bool live_time_tracking =
true;
76 struct tm *
SCLocalTime(time_t timep,
struct tm *result);
77 struct tm *
SCUtcTime(time_t timep,
struct tm *result);
94 if (live_time_tracking)
101 live_time_tracking =
true;
107 live_time_tracking =
false;
113 return live_time_tracking;
118 if (live_time_tracking)
127 if (live_time_tracking)
133 SCLogDebug(
"time set to %" PRIuMAX
" sec, %" PRIuMAX
" usec",
134 (uintmax_t)current_time.tv_sec, (uintmax_t)current_time.tv_usec);
143 memset(&
tv, 0x00,
sizeof(
tv));
145 gettimeofday(&
tv, NULL);
154 struct timeval
tv = { 0 };
155 if (live_time_tracking) {
156 gettimeofday(&
tv, NULL);
161 tv.tv_sec = current_time.tv_sec;
162 tv.tv_usec = current_time.tv_usec;
172 SCLogDebug(
"time we got is %" PRIuMAX
" sec, %" PRIuMAX
" usec", (uintmax_t)
tv.tv_sec,
173 (uintmax_t)
tv.tv_usec);
195 static inline void WinStrftime(
const SCTime_t ts,
const struct tm *t,
char *
str,
size_t size)
197 char time_fmt[64] = { 0 };
199 const long int tzdiff = -_timezone;
200 const int h = abs(_timezone) / 3600 + _daylight;
201 const int m = (abs(_timezone) % 3600) / 60;
202 snprintf(tz,
sizeof(tz),
"%c%02d%02d", tzdiff < 0 ?
'-' :
'+', h,
m);
203 strftime(time_fmt,
sizeof(time_fmt),
"%Y-%m-%dT%H:%M:%S.%%06u", t);
213 memset(&local_tm, 0,
sizeof(local_tm));
214 struct tm *t = (
struct tm*)
SCLocalTime(time, &local_tm);
218 WinStrftime(
ts, t,
str, size);
220 char time_fmt[64] = { 0 };
222 strftime(time_fmt,
sizeof(time_fmt),
"%Y-%m-%dT%H:%M:%S.%%06" PRIi64
"%z", t);
223 snprintf(
str, size, time_fmt, usec);
226 snprintf(
str, size,
"ts-error");
234 memset(&local_tm, 0,
sizeof(local_tm));
235 struct tm *t = (
struct tm*)
SCUtcTime(time, &local_tm);
238 char time_fmt[64] = { 0 };
239 strftime(time_fmt,
sizeof(time_fmt),
"%Y-%m-%dT%H:%M:%S", t);
242 snprintf(
str, size,
"ts-error");
249 strftime(
str, size, fmt, t);
251 snprintf(
str, size,
"ts-error");
257 return gmtime_r(&timep, result);
269 return localtime_r(&timep, result);
276 struct tm *t = (
struct tm*)
SCLocalTime(time, &local_tm);
279 snprintf(
str, size,
"%02d/%02d/%02d-%02d:%02d:%02d.%06u", t->tm_mon + 1, t->tm_mday,
280 t->tm_year + 1900, t->tm_hour, t->tm_min, t->tm_sec, (uint32_t)
SCTIME_USECS(
ts));
282 snprintf(
str, size,
"ts-error");
294 #define MAX_LOCAL_TIME_STRING 32
296 static thread_local
int mru_time_slot;
297 static thread_local time_t last_local_time[2];
298 static thread_local
short int cached_local_time_len[2];
299 static thread_local
char cached_local_time[2][MAX_LOCAL_TIME_STRING];
303 static thread_local
int mru_tm_slot;
304 static thread_local time_t cached_minute_start[2];
305 static thread_local
struct tm cached_local_tm[2];
329 struct tm *
SCLocalTime(time_t timep,
struct tm *result)
333 int mru = mru_tm_slot;
335 int mru_seconds = timep - cached_minute_start[mru];
336 int lru_seconds = timep - cached_minute_start[lru];
338 if (cached_minute_start[mru]==0 && cached_minute_start[lru]==0) {
339 localtime_r(&timep, &cached_local_tm[lru]);
341 new_seconds = cached_local_tm[lru].tm_sec;
342 cached_minute_start[lru] = timep - new_seconds;
345 }
else if (lru_seconds > 0 && (mru_seconds >= 0 && mru_seconds <= 59)) {
347 new_seconds = mru_seconds;
348 }
else if (mru_seconds > 0 && (lru_seconds >= 0 && lru_seconds <= 59)) {
350 new_seconds = lru_seconds;
355 if (localtime_r(&timep, &cached_local_tm[lru]) == NULL)
358 new_seconds = cached_local_tm[lru].tm_sec;
359 cached_minute_start[lru] = timep - new_seconds;
363 memcpy(result, &cached_local_tm[mru],
sizeof(
struct tm));
364 result->tm_sec = new_seconds;
370 static int UpdateCachedTime(
int n, time_t time)
373 struct tm *t = (
struct tm *)
SCLocalTime(time, &local_tm);
374 int cached_len = snprintf(cached_local_time[n], MAX_LOCAL_TIME_STRING,
375 "%02d/%02d/%02d-%02d:%02d:",
376 t->tm_mon + 1, t->tm_mday, t->tm_year + 1900,
377 t->tm_hour, t->tm_min);
378 cached_local_time_len[n] = cached_len;
380 last_local_time[n] = time - t->tm_sec;
399 int mru = mru_time_slot;
401 int mru_seconds = time - last_local_time[mru];
402 int lru_seconds = time - last_local_time[lru];
403 if (last_local_time[mru]==0 && last_local_time[lru]==0) {
405 UpdateCachedTime(mru, time);
406 seconds = UpdateCachedTime(lru, time);
407 }
else if (mru_seconds >= 0 && mru_seconds <= 59) {
409 seconds = mru_seconds;
410 }
else if (lru_seconds >= 0 && lru_seconds <= 59) {
412 seconds = lru_seconds;
417 seconds = UpdateCachedTime(lru, time);
422 char *cached_str = cached_local_time[mru_time_slot];
423 int cached_len = cached_local_time_len[mru_time_slot];
424 if (cached_len >= (
int)size)
426 memcpy(
str, cached_str, cached_len);
427 snprintf(
str + cached_len, size - cached_len,
"%02d.%06u", seconds, (uint32_t)
SCTIME_USECS(
ts));
446 #define MONTHSPERYEAR 12
448 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
451 result = (year - 1970) * 365 + mdays[tp->tm_mon %
MONTHSPERYEAR];
452 result += (year - 1968) / 4;
453 result -= (year - 1900) / 100;
454 result += (year - 1600) / 400;
455 if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0) &&
458 result += tp->tm_mday - 1;
460 result += tp->tm_hour;
462 result += tp->tm_min;
464 result += tp->tm_sec;
467 result -= tp->tm_gmtoff;
492 for (i = 0; i < num_patterns; i++)
494 if (patterns[i] == NULL)
497 tp->tm_hour = tp->tm_min = tp->tm_sec = 0;
498 tp->tm_year = tp->tm_mon = tp->tm_mday = tp->tm_wday = INT_MIN;
504 result =
strptime(
string, patterns[i], tp);
506 if (result && *result ==
'\0')
511 if (result == NULL || *result !=
'\0')
515 if (tp->tm_year == INT_MIN && tp->tm_mon == INT_MIN &&
516 tp->tm_mday == INT_MIN)
521 if (tp->tm_year != INT_MIN && tp->tm_mon != INT_MIN &&
544 memset(&tm, 0,
sizeof(tm));
545 struct tm *tp = (
struct tm *)
SCLocalTime(epoch, &tm);
546 char buffer[PATH_MAX] = { 0 };
552 int r = strftime(buffer,
sizeof(buffer), pattern, tp);
573 uint64_t modifier = 1;
574 char last =
str[strlen(
str)-1];
593 modifier = 60 * 60 * 24;
597 modifier = 60 * 60 * 24 * 7;
605 size = strtoumax(
str, NULL, 10);
610 return (size * modifier);
623 uint64_t seconds = 0;
625 memset(&tm, 0,
sizeof(tm));
626 struct tm *tp = (
struct tm *)
SCLocalTime(epoch, &tm);
628 if (strcmp(
str,
"minute") == 0)
629 seconds = 60 - tp->tm_sec;
630 else if (strcmp(
str,
"hour") == 0)
631 seconds = (60 * (60 - tp->tm_min)) + (60 - tp->tm_sec);
632 else if (strcmp(
str,
"day") == 0)
633 seconds = (3600 * (24 - tp->tm_hour)) + (60 * (60 - tp->tm_min)) +
641 return ts->tv_sec * 1000L +
ts->tv_nsec / 1000000L;
646 return (uint64_t)(t1.tv_sec - t0.tv_sec) * 1000000L + (t1.tv_usec - t1.tv_usec);