Browse Source

convert DB time to UTC correctly, make ckdbYYYYMMDDHH.log UTC and make time use it's own buffer

master
kanoi 10 years ago
parent
commit
7698d1880b
  1. 40
      src/ckdb.c
  2. 16
      src/ckpool.c
  3. 16
      src/ktree.c
  4. 8
      src/libckpool.c

40
src/ckdb.c

@ -1222,22 +1222,22 @@ void logmsg(int loglevel, const char *fmt, ...)
{ {
int logfd = 0; int logfd = 0;
char *buf = NULL; char *buf = NULL;
struct tm *tm; struct tm tm;
time_t now_t; time_t now_t;
va_list ap; va_list ap;
char stamp[128]; char stamp[128];
char *extra = EMPTY; char *extra = EMPTY;
now_t = time(NULL); now_t = time(NULL);
tm = localtime(&now_t); localtime_r(&now_t, &tm);
snprintf(stamp, sizeof(stamp), snprintf(stamp, sizeof(stamp),
"[%d-%02d-%02d %02d:%02d:%02d]", "[%d-%02d-%02d %02d:%02d:%02d]",
tm->tm_year + 1900, tm.tm_year + 1900,
tm->tm_mon + 1, tm.tm_mon + 1,
tm->tm_mday, tm.tm_mday,
tm->tm_hour, tm.tm_hour,
tm->tm_min, tm.tm_min,
tm->tm_sec); tm.tm_sec);
if (!fmt) { if (!fmt) {
fprintf(stderr, "%s %s() called without fmt\n", stamp, __func__); fprintf(stderr, "%s %s() called without fmt\n", stamp, __func__);
@ -1440,16 +1440,17 @@ static void _txt_to_data(enum data_type typ, char *nam, char *fld, void *data, s
nam, (int)siz, (int)sizeof(tv_t), WHERE_FFL_PASS); nam, (int)siz, (int)sizeof(tv_t), WHERE_FFL_PASS);
} }
unsigned int yyyy, mm, dd, HH, MM, SS, uS = 0, tz; unsigned int yyyy, mm, dd, HH, MM, SS, uS = 0, tz;
char pm[2];
struct tm tm; struct tm tm;
time_t tim; time_t tim;
int n; int n;
n = sscanf(fld, "%u-%u-%u %u:%u:%u+%u", n = sscanf(fld, "%u-%u-%u %u:%u:%u%1[+-]%u",
&yyyy, &mm, &dd, &HH, &MM, &SS, &tz); &yyyy, &mm, &dd, &HH, &MM, &SS, pm, &tz);
if (n != 7) { if (n != 8) {
// allow uS // allow uS
n = sscanf(fld, "%u-%u-%u %u:%u:%u.%u+%u", n = sscanf(fld, "%u-%u-%u %u:%u:%u.%u%1[+-]%u",
&yyyy, &mm, &dd, &HH, &MM, &SS, &uS, &tz); &yyyy, &mm, &dd, &HH, &MM, &SS, &uS, pm, &tz);
if (n != 8) { if (n != 9) {
quithere(1, "Field %s timeval unhandled date '%s' (%d)" WHERE_FFL, quithere(1, "Field %s timeval unhandled date '%s' (%d)" WHERE_FFL,
nam, fld, n, WHERE_FFL_PASS); nam, fld, n, WHERE_FFL_PASS);
} }
@ -1461,12 +1462,19 @@ static void _txt_to_data(enum data_type typ, char *nam, char *fld, void *data, s
tm.tm_mon = (int)mm - 1; tm.tm_mon = (int)mm - 1;
tm.tm_year = (int)yyyy - 1900; tm.tm_year = (int)yyyy - 1900;
tm.tm_isdst = -1; tm.tm_isdst = -1;
tim = mktime(&tm); tim = timegm(&tm);
// Fix TZ offsets errors
if (tim > COMPARE_EXPIRY) { if (tim > COMPARE_EXPIRY) {
((tv_t *)data)->tv_sec = default_expiry.tv_sec; ((tv_t *)data)->tv_sec = default_expiry.tv_sec;
((tv_t *)data)->tv_usec = default_expiry.tv_usec; ((tv_t *)data)->tv_usec = default_expiry.tv_usec;
} else { } else {
// 2 digit tz (vs 4 digit)
if (tz < 25)
tz *= 60;
// time was converted ignoring tz - so correct it
if (pm[0] == '-')
tim += 60 * tz;
else
tim -= 60 * tz;
((tv_t *)data)->tv_sec = tim; ((tv_t *)data)->tv_sec = tim;
((tv_t *)data)->tv_usec = uS; ((tv_t *)data)->tv_usec = uS;
} }

16
src/ckpool.c

@ -63,7 +63,7 @@ void logmsg(int loglevel, const char *fmt, ...) {
if (global_ckp->loglevel >= loglevel && fmt) { if (global_ckp->loglevel >= loglevel && fmt) {
int logfd = global_ckp->logfd; int logfd = global_ckp->logfd;
char *buf = NULL; char *buf = NULL;
struct tm *tm; struct tm tm;
time_t now_t; time_t now_t;
va_list ap; va_list ap;
char stamp[128]; char stamp[128];
@ -73,14 +73,14 @@ void logmsg(int loglevel, const char *fmt, ...) {
va_end(ap); va_end(ap);
now_t = time(NULL); now_t = time(NULL);
tm = localtime(&now_t); localtime_r(&now_t, &tm);
sprintf(stamp, "[%d-%02d-%02d %02d:%02d:%02d]", sprintf(stamp, "[%d-%02d-%02d %02d:%02d:%02d]",
tm->tm_year + 1900, tm.tm_year + 1900,
tm->tm_mon + 1, tm.tm_mon + 1,
tm->tm_mday, tm.tm_mday,
tm->tm_hour, tm.tm_hour,
tm->tm_min, tm.tm_min,
tm->tm_sec); tm.tm_sec);
if (logfd) { if (logfd) {
char *msg; char *msg;

16
src/ktree.c

@ -129,20 +129,20 @@ void _dsp_ktree(K_LIST *list, K_TREE *root, char *filename, char *msg, KTREE_FFL
K_TREE_CTX ctx[1]; K_TREE_CTX ctx[1];
K_ITEM *item; K_ITEM *item;
FILE *stream; FILE *stream;
struct tm *tm; struct tm tm;
time_t now_t; time_t now_t;
char stamp[128]; char stamp[128];
now_t = time(NULL); now_t = time(NULL);
tm = localtime(&now_t); localtime_r(&now_t, &tm);
snprintf(stamp, sizeof(stamp), snprintf(stamp, sizeof(stamp),
"[%d-%02d-%02d %02d:%02d:%02d]", "[%d-%02d-%02d %02d:%02d:%02d]",
tm->tm_year + 1900, tm.tm_year + 1900,
tm->tm_mon + 1, tm.tm_mon + 1,
tm->tm_mday, tm.tm_mday,
tm->tm_hour, tm.tm_hour,
tm->tm_min, tm.tm_min,
tm->tm_sec); tm.tm_sec);
stream = fopen(filename, "a"); stream = fopen(filename, "a");
if (!stream) if (!stream)

8
src/libckpool.c

@ -834,11 +834,11 @@ json_t *json_object_dup(json_t *val, const char *entry)
char *rotating_filename(const char *path, time_t when) char *rotating_filename(const char *path, time_t when)
{ {
char *filename; char *filename;
struct tm *tm; struct tm tm;
tm = localtime(&when); gmtime_r(&when, &tm);
ASPRINTF(&filename, "%s%04d%02d%02d%02d.log", path, tm->tm_year + 1900, tm->tm_mon + 1, ASPRINTF(&filename, "%s%04d%02d%02d%02d.log", path, tm.tm_year + 1900, tm.tm_mon + 1,
tm->tm_mday, tm->tm_hour); tm.tm_mday, tm.tm_hour);
return filename; return filename;
} }

Loading…
Cancel
Save