From 7698d1880ba7a9823a6bdd966cd3d8631e1c0def Mon Sep 17 00:00:00 2001 From: kanoi Date: Thu, 7 Aug 2014 02:19:58 +1000 Subject: [PATCH] convert DB time to UTC correctly, make ckdbYYYYMMDDHH.log UTC and make time use it's own buffer --- src/ckdb.c | 40 ++++++++++++++++++++++++---------------- src/ckpool.c | 16 ++++++++-------- src/ktree.c | 16 ++++++++-------- src/libckpool.c | 8 ++++---- 4 files changed, 44 insertions(+), 36 deletions(-) diff --git a/src/ckdb.c b/src/ckdb.c index a2f5a7b6..0cbe5ff2 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -1222,22 +1222,22 @@ void logmsg(int loglevel, const char *fmt, ...) { int logfd = 0; char *buf = NULL; - struct tm *tm; + struct tm tm; time_t now_t; va_list ap; char stamp[128]; char *extra = EMPTY; now_t = time(NULL); - tm = localtime(&now_t); + localtime_r(&now_t, &tm); snprintf(stamp, sizeof(stamp), "[%d-%02d-%02d %02d:%02d:%02d]", - tm->tm_year + 1900, - tm->tm_mon + 1, - tm->tm_mday, - tm->tm_hour, - tm->tm_min, - tm->tm_sec); + tm.tm_year + 1900, + tm.tm_mon + 1, + tm.tm_mday, + tm.tm_hour, + tm.tm_min, + tm.tm_sec); if (!fmt) { 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); } unsigned int yyyy, mm, dd, HH, MM, SS, uS = 0, tz; + char pm[2]; struct tm tm; time_t tim; int n; - n = sscanf(fld, "%u-%u-%u %u:%u:%u+%u", - &yyyy, &mm, &dd, &HH, &MM, &SS, &tz); - if (n != 7) { + n = sscanf(fld, "%u-%u-%u %u:%u:%u%1[+-]%u", + &yyyy, &mm, &dd, &HH, &MM, &SS, pm, &tz); + if (n != 8) { // allow uS - n = sscanf(fld, "%u-%u-%u %u:%u:%u.%u+%u", - &yyyy, &mm, &dd, &HH, &MM, &SS, &uS, &tz); - if (n != 8) { + n = sscanf(fld, "%u-%u-%u %u:%u:%u.%u%1[+-]%u", + &yyyy, &mm, &dd, &HH, &MM, &SS, &uS, pm, &tz); + if (n != 9) { quithere(1, "Field %s timeval unhandled date '%s' (%d)" WHERE_FFL, 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_year = (int)yyyy - 1900; tm.tm_isdst = -1; - tim = mktime(&tm); - // Fix TZ offsets errors + tim = timegm(&tm); if (tim > COMPARE_EXPIRY) { ((tv_t *)data)->tv_sec = default_expiry.tv_sec; ((tv_t *)data)->tv_usec = default_expiry.tv_usec; } 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_usec = uS; } diff --git a/src/ckpool.c b/src/ckpool.c index 9f9d333a..4dcc607b 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -63,7 +63,7 @@ void logmsg(int loglevel, const char *fmt, ...) { if (global_ckp->loglevel >= loglevel && fmt) { int logfd = global_ckp->logfd; char *buf = NULL; - struct tm *tm; + struct tm tm; time_t now_t; va_list ap; char stamp[128]; @@ -73,14 +73,14 @@ void logmsg(int loglevel, const char *fmt, ...) { va_end(ap); now_t = time(NULL); - tm = localtime(&now_t); + localtime_r(&now_t, &tm); sprintf(stamp, "[%d-%02d-%02d %02d:%02d:%02d]", - tm->tm_year + 1900, - tm->tm_mon + 1, - tm->tm_mday, - tm->tm_hour, - tm->tm_min, - tm->tm_sec); + tm.tm_year + 1900, + tm.tm_mon + 1, + tm.tm_mday, + tm.tm_hour, + tm.tm_min, + tm.tm_sec); if (logfd) { char *msg; diff --git a/src/ktree.c b/src/ktree.c index b1648c77..e823ee6d 100644 --- a/src/ktree.c +++ b/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_ITEM *item; FILE *stream; - struct tm *tm; + struct tm tm; time_t now_t; char stamp[128]; now_t = time(NULL); - tm = localtime(&now_t); + localtime_r(&now_t, &tm); snprintf(stamp, sizeof(stamp), "[%d-%02d-%02d %02d:%02d:%02d]", - tm->tm_year + 1900, - tm->tm_mon + 1, - tm->tm_mday, - tm->tm_hour, - tm->tm_min, - tm->tm_sec); + tm.tm_year + 1900, + tm.tm_mon + 1, + tm.tm_mday, + tm.tm_hour, + tm.tm_min, + tm.tm_sec); stream = fopen(filename, "a"); if (!stream) diff --git a/src/libckpool.c b/src/libckpool.c index fbc8e91c..2ad19c3d 100644 --- a/src/libckpool.c +++ b/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 *filename; - struct tm *tm; + struct tm tm; - tm = localtime(&when); - ASPRINTF(&filename, "%s%04d%02d%02d%02d.log", path, tm->tm_year + 1900, tm->tm_mon + 1, - tm->tm_mday, tm->tm_hour); + gmtime_r(&when, &tm); + ASPRINTF(&filename, "%s%04d%02d%02d%02d.log", path, tm.tm_year + 1900, tm.tm_mon + 1, + tm.tm_mday, tm.tm_hour); return filename; }