summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 9097c9c)
raw | patch | inline | side by side (parent: 9097c9c)
author | Sebastian Harl <sh@tokkee.org> | |
Wed, 20 May 2015 21:53:48 +0000 (23:53 +0200) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Wed, 20 May 2015 21:53:48 +0000 (23:53 +0200) |
Also, print the nanosecond fraction of a second.
diff --git a/src/core/data.c b/src/core/data.c
index 444f5a1c91053d7f30665603fccaea394281d33e..58c8b524e1111c662e7587aa5e1614d10d084b5d 100644 (file)
--- a/src/core/data.c
+++ b/src/core/data.c
/* in the worst case, each character needs to be escaped */
return 2 * strlen(datum->data.string) + 2;
case SDB_TYPE_DATETIME:
- /* "YYYY-MM-DD HH:MM:SS +zzzz" */
- return 27;
+ /* "YYYY-MM-DD HH:MM:SS[.nnnnnnnnn] +zzzz" */
+ return 37;
case SDB_TYPE_BINARY:
if (! datum->data.binary.datum)
return 6; /* NULL */
@@ -1013,8 +1013,7 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
}
}
else if (datum->type == SDB_TYPE_DATETIME) {
- if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
- datum->data.datetime))
+ if (! sdb_strftime(tmp, sizeof(tmp), datum->data.datetime))
return -1;
tmp[sizeof(tmp) - 1] = '\0';
data = tmp;
diff --git a/src/core/store.c b/src/core/store.c
index bf12a9378eb228e811a87ea268eb8fa403991672..171e31935bb898eefb5e6e95b46000a20f3cb1a6 100644 (file)
--- a/src/core/store.c
+++ b/src/core/store.c
size_t i;
/* TODO: make time format configurable */
- if (! sdb_strftime(start_str, sizeof(start_str),
- "%F %T %z", ts->start))
+ if (! sdb_strftime(start_str, sizeof(start_str), ts->start))
snprintf(start_str, sizeof(start_str), "<error>");
start_str[sizeof(start_str) - 1] = '\0';
- if (! sdb_strftime(end_str, sizeof(end_str),
- "%F %T %z", ts->end))
+ if (! sdb_strftime(end_str, sizeof(end_str), ts->end))
snprintf(end_str, sizeof(end_str), "<error>");
end_str[sizeof(end_str) - 1] = '\0';
for (j = 0; j < ts->data_len; ++j) {
char time_str[64];
- if (! sdb_strftime(time_str, sizeof(time_str),
- "%F %T %z", ts->data[i][j].timestamp))
+ if (! sdb_strftime(time_str, sizeof(time_str), ts->data[i][j].timestamp))
snprintf(time_str, sizeof(time_str), "<error>");
time_str[sizeof(time_str) - 1] = '\0';
diff --git a/src/core/store_json.c b/src/core/store_json.c
index a772dc39c8f67549c8205a0f63527672b6edf473..7362691398af584ed3725d220cbebb85a26d6906 100644 (file)
--- a/src/core/store_json.c
+++ b/src/core/store_json.c
}
/* TODO: make time and interval formats configurable */
- if (! sdb_strftime(time_str, sizeof(time_str),
- "%F %T %z", obj->last_update))
+ if (! sdb_strftime(time_str, sizeof(time_str), obj->last_update))
snprintf(time_str, sizeof(time_str), "<error>");
time_str[sizeof(time_str) - 1] = '\0';
diff --git a/src/core/time.c b/src/core/time.c
index d0ed0b8dd9917d11596fa54da2f65071026c968f..d8e8fabf6442e3d83ac02364521c9a1abd54e98e 100644 (file)
--- a/src/core/time.c
+++ b/src/core/time.c
} /* sdb_sleep */
size_t
-sdb_strftime(char *s, size_t len, const char *format, sdb_time_t t)
+sdb_strftime(char *s, size_t len, sdb_time_t t)
{
+ char tmp[len];
time_t tstamp;
struct tm tm;
+ long tz;
memset(&tm, 0, sizeof(tm));
-
tstamp = (time_t)SDB_TIME_TO_SECS(t);
if (! localtime_r (&tstamp, &tm))
return 0;
- return strftime(s, len, format, &tm);
+ if (! strftime(tmp, len, "%F %T", &tm))
+ return 0;
+ tmp[sizeof(tmp) - 1] = '\0';
+
+ tz = -timezone / 36;
+ if (tm.tm_isdst > 0)
+ tz += 100;
+
+ t %= SDB_INTERVAL_SECOND;
+ if (! t)
+ return snprintf(s, len, "%s %+05ld", tmp, tz);
+ return snprintf(s, len, "%s.%09ld %+05ld", tmp, t, tz);
} /* sdb_strftime */
size_t
index c48a2e1329b6bb8371507fa382dcf90601591a18..a9ca72f01a2dbfecc34a9251011f1b3afccb0994 100644 (file)
--- a/src/include/core/time.h
+++ b/src/include/core/time.h
sdb_sleep(sdb_time_t reg, sdb_time_t *rem);
size_t
-sdb_strftime(char *s, size_t len, const char *format, sdb_time_t)
- __attribute__((format(strftime, 3, 0)));
+sdb_strftime(char *s, size_t len, sdb_time_t);
size_t
sdb_strfinterval(char *s, size_t len, sdb_time_t interval);
diff --git a/src/parser/analyzer.c b/src/parser/analyzer.c
index dea83d7e0c828d8966fbffe9e91a2895c466fdf7..9bbd9ba7c9fe31c1575a1160f3ffd218c6ba04fa 100644 (file)
--- a/src/parser/analyzer.c
+++ b/src/parser/analyzer.c
}
if (ts->end <= ts->start) {
char start_str[64], end_str[64];
- sdb_strftime(start_str, sizeof(start_str), "%F %T Tz", ts->start);
- sdb_strftime(end_str, sizeof(end_str), "%F %T Tz", ts->end);
+ sdb_strftime(start_str, sizeof(start_str), ts->start);
+ sdb_strftime(end_str, sizeof(end_str), ts->end);
sdb_strbuf_sprintf(errbuf, "Start time (%s) greater than "
"end time (%s) in STORE command", start_str, end_str);
return -1;
index d421e64df3dd90136776a9b84925ccccf689ffab..27daaae149a1bf48dbc7b2b9e0a50a87ae4a4dff 100644 (file)
--- a/t/unit/core/data_test.c
+++ b/t/unit/core/data_test.c
},
{
{ SDB_TYPE_DATETIME, { .datetime= 471147114711471100 } },
- "\"1984-12-06 02:11:54 +0000\"",
+ "\"1984-12-06 02:11:54.711471100 +0000\"",
},
{
{ SDB_TYPE_BINARY, { .binary = { 0, NULL } } },
index b69573c45fde0afd0b90ab5db4402f225eb9ac00..113d3a0010d0f5669e53d68aa6dbd2fdad753858 100644 (file)
#include <check.h>
#include <stdlib.h>
+/* Make SDB_INTERVAL_SECOND a constant initializer. */
+#undef SDB_INTERVAL_SECOND
+#define SDB_INTERVAL_SECOND 1000000000L
+
static void
populate(void)
{
sdb_data_t datum;
- sdb_store_host("h1", 1);
- sdb_store_host("h2", 3);
+ sdb_store_host("h1", 1 * SDB_INTERVAL_SECOND);
+ sdb_store_host("h2", 3 * SDB_INTERVAL_SECOND);
datum.type = SDB_TYPE_STRING;
datum.data.string = "v1";
- sdb_store_attribute("h1", "k1", &datum, 1);
+ sdb_store_attribute("h1", "k1", &datum, 1 * SDB_INTERVAL_SECOND);
datum.data.string = "v2";
- sdb_store_attribute("h1", "k2", &datum, 2);
+ sdb_store_attribute("h1", "k2", &datum, 2 * SDB_INTERVAL_SECOND);
datum.data.string = "v3";
- sdb_store_attribute("h1", "k3", &datum, 2);
+ sdb_store_attribute("h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
/* make sure that older updates don't overwrite existing values */
datum.data.string = "fail";
- sdb_store_attribute("h1", "k2", &datum, 1);
- sdb_store_attribute("h1", "k3", &datum, 2);
+ sdb_store_attribute("h1", "k2", &datum, 1 * SDB_INTERVAL_SECOND);
+ sdb_store_attribute("h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
- sdb_store_metric("h1", "m1", /* store */ NULL, 2);
- sdb_store_metric("h1", "m2", /* store */ NULL, 1);
- sdb_store_metric("h2", "m1", /* store */ NULL, 1);
+ sdb_store_metric("h1", "m1", /* store */ NULL, 2 * SDB_INTERVAL_SECOND);
+ sdb_store_metric("h1", "m2", /* store */ NULL, 1 * SDB_INTERVAL_SECOND);
+ sdb_store_metric("h2", "m1", /* store */ NULL, 1 * SDB_INTERVAL_SECOND);
- sdb_store_service("h2", "s1", 1);
- sdb_store_service("h2", "s2", 2);
+ sdb_store_service("h2", "s1", 1 * SDB_INTERVAL_SECOND);
+ sdb_store_service("h2", "s2", 2 * SDB_INTERVAL_SECOND);
datum.type = SDB_TYPE_INTEGER;
datum.data.integer = 42;
- sdb_store_metric_attr("h1", "m1", "k3", &datum, 2);
+ sdb_store_metric_attr("h1", "m1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
datum.data.integer = 123;
- sdb_store_service_attr("h2", "s2", "k1", &datum, 2);
+ sdb_store_service_attr("h2", "s2", "k1", &datum, 2 * SDB_INTERVAL_SECOND);
datum.data.integer = 4711;
- sdb_store_service_attr("h2", "s2", "k2", &datum, 1);
+ sdb_store_service_attr("h2", "s2", "k2", &datum, 1 * SDB_INTERVAL_SECOND);
/* don't overwrite k1 */
datum.data.integer = 666;
- sdb_store_service_attr("h2", "s2", "k1", &datum, 2);
+ sdb_store_service_attr("h2", "s2", "k1", &datum, 2 * SDB_INTERVAL_SECOND);
} /* populate */
static int
{ { NULL, 0, SDB_DATA_INIT },
SDB_HOST, scan_tojson_full,
"["
- "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": ["
"{\"name\": \"k1\", \"value\": \"v1\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []},"
"{\"name\": \"k2\", \"value\": \"v2\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []},"
"{\"name\": \"k3\", \"value\": \"v3\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"], "
"\"metrics\": ["
"{\"name\": \"m1\", "
"\"timeseries\": false, "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": ["
"{\"name\": \"hostname\", \"value\": \"h1\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []},"
"{\"name\": \"k3\", \"value\": 42, "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]},"
"{\"name\": \"m2\", "
"\"timeseries\": false, "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": ["
"{\"name\": \"hostname\", \"value\": \"h1\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]}"
"]},"
- "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"metrics\": ["
"{\"name\": \"m1\", "
"\"timeseries\": false, "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": ["
"{\"name\": \"hostname\", \"value\": \"h2\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]}"
"], "
"\"services\": ["
"{\"name\": \"s1\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": ["
"{\"name\": \"hostname\", \"value\": \"h2\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]},"
"{\"name\": \"s2\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": ["
"{\"name\": \"hostname\", \"value\": \"h2\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []},"
"{\"name\": \"k1\", \"value\": 123, "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []},"
"{\"name\": \"k2\", \"value\": 4711, "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]}"
"]}"
{ { NULL, 0, SDB_DATA_INIT },
SDB_HOST, scan_tojson,
"["
- "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []},"
- "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]" },
{ { sdb_store_eq_matcher, SDB_FIELD_NAME,
{ SDB_TYPE_STRING, { .string = "h1" } } },
SDB_HOST, scan_tojson_full,
"["
- "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]" },
{ { sdb_store_gt_matcher, SDB_FIELD_LAST_UPDATE,
- { SDB_TYPE_DATETIME, { .datetime = 1 } } },
+ { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } },
SDB_HOST, scan_tojson_full,
"["
- "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"services\": ["
"{\"name\": \"s2\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": ["
"{\"name\": \"hostname\", \"value\": \"h2\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []},"
"{\"name\": \"k1\", \"value\": 123, "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]}"
"]}"
"]" },
{ { sdb_store_le_matcher, SDB_FIELD_LAST_UPDATE,
- { SDB_TYPE_DATETIME, { .datetime = 1 } } },
+ { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } },
SDB_HOST, scan_tojson_full,
"["
- "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": ["
"{\"name\": \"k1\", \"value\": \"v1\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"], "
"\"metrics\": ["
"{\"name\": \"m2\", "
"\"timeseries\": false, "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": ["
"{\"name\": \"hostname\", \"value\": \"h1\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]}"
"]}"
"]" },
{ { sdb_store_ge_matcher, SDB_FIELD_LAST_UPDATE,
- { SDB_TYPE_DATETIME, { .datetime = 3 } } },
+ { SDB_TYPE_DATETIME, { .datetime = 3 * SDB_INTERVAL_SECOND } } },
SDB_HOST, scan_tojson_full,
"["
- "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]" },
{ { sdb_store_lt_matcher, SDB_FIELD_LAST_UPDATE,
{ { NULL, 0, SDB_DATA_INIT },
SDB_SERVICE, scan_tojson_full,
"["
- "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"services\": ["
"{\"name\": \"s1\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": ["
"{\"name\": \"hostname\", \"value\": \"h2\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]},"
"{\"name\": \"s2\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": ["
"{\"name\": \"hostname\", \"value\": \"h2\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []},"
"{\"name\": \"k1\", \"value\": 123, "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []},"
"{\"name\": \"k2\", \"value\": 4711, "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]}"
"]}"
{ { NULL, 0, SDB_DATA_INIT },
SDB_SERVICE, scan_tojson,
"["
- "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"services\": ["
"{\"name\": \"s1\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []},"
"{\"name\": \"s2\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]}"
"]" },
{ { sdb_store_gt_matcher, SDB_FIELD_LAST_UPDATE,
- { SDB_TYPE_DATETIME, { .datetime = 1 } } },
+ { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } },
SDB_SERVICE, scan_tojson_full,
"["
- "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"services\": ["
"{\"name\": \"s2\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": ["
"{\"name\": \"hostname\", \"value\": \"h2\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []},"
"{\"name\": \"k1\", \"value\": 123, "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]}"
"]}"
{ { NULL, 0, SDB_DATA_INIT },
SDB_METRIC, scan_tojson_full,
"["
- "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"metrics\": ["
"{\"name\": \"m1\", "
"\"timeseries\": false, "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": ["
"{\"name\": \"hostname\", \"value\": \"h1\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []},"
"{\"name\": \"k3\", \"value\": 42, "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]},"
"{\"name\": \"m2\", "
"\"timeseries\": false, "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": ["
"{\"name\": \"hostname\", \"value\": \"h1\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]}"
"]},"
- "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"metrics\": ["
"{\"name\": \"m1\", "
"\"timeseries\": false, "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": ["
"{\"name\": \"hostname\", \"value\": \"h2\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]}"
"]}"
{ { NULL, 0, SDB_DATA_INIT },
SDB_METRIC, scan_tojson,
"["
- "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"metrics\": ["
"{\"name\": \"m1\", "
"\"timeseries\": false, "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []},"
"{\"name\": \"m2\", "
"\"timeseries\": false, "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]},"
- "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"metrics\": ["
"{\"name\": \"m1\", "
"\"timeseries\": false, "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]}"
"]" },
{ { sdb_store_le_matcher, SDB_FIELD_LAST_UPDATE,
- { SDB_TYPE_DATETIME, { .datetime = 1 } } },
+ { SDB_TYPE_DATETIME, { .datetime = 1 * SDB_INTERVAL_SECOND } } },
SDB_METRIC, scan_tojson_full,
"["
- "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"metrics\": ["
"{\"name\": \"m2\", "
"\"timeseries\": false, "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": [], "
"\"attributes\": ["
"{\"name\": \"hostname\", \"value\": \"h1\", "
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", "
"\"update_interval\": \"0s\", \"backends\": []}"
"]}"
"]}"
index 9fee9dbd46303987b02b3a814010f80d59612296..978fc02d6f99eb47636391e975520489f3fbfe9b 100644 (file)
--- a/t/unit/core/time_test.c
+++ b/t/unit/core/time_test.c
#define US 1000L
#define NS 1L
+struct {
+ sdb_time_t t;
+ const char *tz;
+ const char *expected;
+} strftime_data[] = {
+ { 0, "UTC", "1970-01-01 00:00:00 +0000" },
+ { 1428066243000000000L, "Europe/Berlin", "2015-04-03 15:04:03 +0200" },
+ { 1420113661000000000L, "Europe/Berlin", "2015-01-01 13:01:01 +0100" },
+ { 1428066243000000000L, "US/Pacific", "2015-04-03 06:04:03 -0700" },
+ { 1420113661000000000L, "US/Pacific", "2015-01-01 04:01:01 -0800" },
+ { 1146747723000000123L, "UTC", "2006-05-04 13:02:03.000000123 +0000" },
+ { 1146747723123456789L, "UTC", "2006-05-04 13:02:03.123456789 +0000" },
+};
+
+START_TEST(test_strftime)
+{
+ char buf[1024], tz[64];
+ size_t check;
+
+ /* strftime does not provide the number of bytes that would have been
+ * written. Only check that it does not segfault. */
+ sdb_strftime(NULL, 0, strftime_data[_i].t);
+
+ snprintf(tz, sizeof(tz), "TZ=%s", strftime_data[_i].tz);
+ putenv(tz);
+ tzset();
+
+ check = sdb_strftime(buf, sizeof(buf), strftime_data[_i].t);
+ fail_unless(check > 0,
+ "%s; sdb_strftime(<buf>, <size>, %"PRIsdbTIME") = %zu; "
+ "expected: >0", tz, strftime_data[_i].t, check);
+ fail_unless(!strcmp(buf, strftime_data[_i].expected),
+ "%s; sdb_strftime(<buf>, <size>, %"PRIsdbTIME") did not "
+ "format time correctly; got: '%s'; expected: '%s'",
+ tz, strftime_data[_i].t, buf, strftime_data[_i].expected);
+ fail_unless(check == strlen(strftime_data[_i].expected),
+ "%s; sdb_strftime(<buf>, <size>, %"PRIsdbTIME") = %zu; "
+ "expected: %zu", tz, strftime_data[_i].t, check,
+ strlen(strftime_data[_i].expected));
+
+ putenv("TZ=UTC");
+ tzset();
+}
+END_TEST
+
struct {
sdb_time_t interval;
const char *expected;
TEST_MAIN("core::time")
{
TCase *tc = tcase_create("core");
+ TC_ADD_LOOP_TEST(tc, strftime);
TC_ADD_LOOP_TEST(tc, strfinterval);
TC_ADD_LOOP_TEST(tc, strpunit);
ADD_TCASE(tc);
index 861313f5363d9094897b83f967f5782f9ae772f2..8872e5b6e13a0f2879430e20e65f7c7f1bf27ebf 100644 (file)
{
sdb_data_t datum;
- sdb_store_host("h1", 1);
- sdb_store_host("h2", 3);
+ sdb_store_host("h1", 1 * SDB_INTERVAL_SECOND);
+ sdb_store_host("h2", 3 * SDB_INTERVAL_SECOND);
datum.type = SDB_TYPE_STRING;
datum.data.string = "v1";
- sdb_store_attribute("h1", "k1", &datum, 1);
+ sdb_store_attribute("h1", "k1", &datum, 1 * SDB_INTERVAL_SECOND);
datum.data.string = "v2";
- sdb_store_attribute("h1", "k2", &datum, 2);
+ sdb_store_attribute("h1", "k2", &datum, 2 * SDB_INTERVAL_SECOND);
datum.data.string = "v3";
- sdb_store_attribute("h1", "k3", &datum, 2);
+ sdb_store_attribute("h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
- sdb_store_metric("h1", "m1", /* store */ NULL, 2);
- sdb_store_metric("h1", "m2", /* store */ NULL, 1);
- sdb_store_metric("h2", "m1", /* store */ NULL, 1);
+ sdb_store_metric("h1", "m1", /* store */ NULL, 2 * SDB_INTERVAL_SECOND);
+ sdb_store_metric("h1", "m2", /* store */ NULL, 1 * SDB_INTERVAL_SECOND);
+ sdb_store_metric("h2", "m1", /* store */ NULL, 1 * SDB_INTERVAL_SECOND);
datum.type = SDB_TYPE_INTEGER;
datum.data.integer = 42;
- sdb_store_metric_attr("h1", "m1", "k3", &datum, 2);
+ sdb_store_metric_attr("h1", "m1", "k3", &datum, 2 * SDB_INTERVAL_SECOND);
- sdb_store_service("h2", "s1", 1);
- sdb_store_service("h2", "s2", 2);
+ sdb_store_service("h2", "s1", 1 * SDB_INTERVAL_SECOND);
+ sdb_store_service("h2", "s2", 2 * SDB_INTERVAL_SECOND);
datum.data.integer = 123;
- sdb_store_service_attr("h2", "s2", "k1", &datum, 2);
+ sdb_store_service_attr("h2", "s2", "k1", &datum, 2 * SDB_INTERVAL_SECOND);
datum.data.integer = 4711;
- sdb_store_service_attr("h2", "s2", "k2", &datum, 1);
+ sdb_store_service_attr("h2", "s2", "k2", &datum, 1 * SDB_INTERVAL_SECOND);
} /* populate */
#define HOST_H1 \
- "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": [], " \
"\"attributes\": [" \
"{\"name\": \"k1\", \"value\": \"v1\", " \
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": []}," \
"{\"name\": \"k2\", \"value\": \"v2\", " \
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": []}," \
"{\"name\": \"k3\", \"value\": \"v3\", " \
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": []}], " \
"\"metrics\": [" \
"{\"name\": \"m1\", \"timeseries\": false, " \
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": [], " \
"\"attributes\": [" \
"{\"name\": \"hostname\", \"value\": \"h1\", " \
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": []}," \
"{\"name\": \"k3\", \"value\": 42, " \
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": []}]}," \
"{\"name\": \"m2\", \"timeseries\": false, " \
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": [], " \
"\"attributes\": [" \
"{\"name\": \"hostname\", \"value\": \"h1\", " \
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": []}]}]}"
#define HOST_H1_ARRAY "["HOST_H1"]"
#define HOST_H1_LISTING \
- "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": []}"
#define HOST_H2_LISTING \
- "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": []}"
#define SERVICE_H2_S1 \
- "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": [], " \
"\"services\": [" \
- "{\"name\": \"s1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "{\"name\": \"s1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": [], " \
"\"attributes\": [" \
"{\"name\": \"hostname\", \"value\": \"h2\", " \
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": []}]}]}"
#define SERVICE_H2_S1_ARRAY "["SERVICE_H2_S1"]"
#define SERVICE_H2_S12_LISTING \
- "[{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "[{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": [], " \
"\"services\": [" \
- "{\"name\": \"s1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "{\"name\": \"s1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": []}," \
- "{\"name\": \"s2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "{\"name\": \"s2\", \"last_update\": \"1970-01-01 00:00:02 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": []}]}]"
#define METRIC_H1_M1 \
- "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": [], " \
"\"metrics\": [" \
"{\"name\": \"m1\", \"timeseries\": false, " \
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": [], " \
"\"attributes\": [" \
"{\"name\": \"hostname\", \"value\": \"h1\", " \
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": []}," \
"{\"name\": \"k3\", \"value\": 42, " \
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": []}]}]}"
#define METRIC_H12_M1_ARRAY \
"["METRIC_H1_M1"," \
- "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": [], " \
"\"metrics\": [" \
"{\"name\": \"m1\", \"timeseries\": false, " \
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": [], " \
"\"attributes\": [" \
"{\"name\": \"hostname\", \"value\": \"h2\", " \
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": []}]}]}]"
#define METRIC_H12_M12_LISTING \
- "[{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "[{\"name\": \"h1\", \"last_update\": \"1970-01-01 00:00:01 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": [], " \
"\"metrics\": [" \
"{\"name\": \"m1\", \"timeseries\": false, " \
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "\"last_update\": \"1970-01-01 00:00:02 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": []}," \
"{\"name\": \"m2\", \"timeseries\": false, " \
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": []}]}," \
- "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "{\"name\": \"h2\", \"last_update\": \"1970-01-01 00:00:03 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": [], " \
"\"metrics\": [" \
"{\"name\": \"m1\", \"timeseries\": false, " \
- "\"last_update\": \"1970-01-01 00:00:00 +0000\", " \
+ "\"last_update\": \"1970-01-01 00:00:01 +0000\", " \
"\"update_interval\": \"0s\", \"backends\": []}]}]"
typedef struct {
diff --git a/t/unit/testutils.h b/t/unit/testutils.h
index a6989655e1c5d481426cab9ed83e750bf31bc8f6..5e0cc853d101b06c9aca1268665cc5807517d44b 100644 (file)
--- a/t/unit/testutils.h
+++ b/t/unit/testutils.h
#include <check.h>
#include <stdio.h>
#include <stdlib.h>
+#include <time.h>
#define TEST_MAIN(name) \
int main(void) \
Suite *s; \
int failed; \
putenv("TZ=UTC"); \
+ tzset(); \
s = suite_create(name); \
sr = srunner_create(s); \