diff --git a/server/include/utils.h b/server/include/utils.h index 4aa77b72d..86730efa8 100644 --- a/server/include/utils.h +++ b/server/include/utils.h @@ -101,6 +101,10 @@ char * strcatf(char **dest, const char *fmt, ...) char * vstrcatf(char **dest, const char *fmt, va_list va) __attribute__ ((format(printf, 2, 0))); +/** Format a struct timespec date similar to strftime() */ +int strftimespec(char *s, size_t max, const char *format, struct timespec *ts) + __attribute__ ((format(strftime, 3, 0))); + /** Convert integer to cpu_set_t. * * @param set A cpu bitmask @@ -129,10 +133,6 @@ int version_compare(struct version *a, struct version *b); /** Parse a dotted version string. */ int version_parse(const char *s, struct version *v); -/** Format a struct timespec date similar to strftime() */ -int strftimespec(char *s, uint max, const char *format, struct timespec *ts) - __attribute__ ((format(strftime, 3, 0))); - /** Check assertion and exit if failed. */ #define assert(exp) do { \ if (!EXPECT(exp, 0)) \ diff --git a/server/src/ngsi.c b/server/src/ngsi.c index 8a9ec3b46..db0445b69 100644 --- a/server/src/ngsi.c +++ b/server/src/ngsi.c @@ -38,15 +38,9 @@ static json_t * json_uuid() static json_t * json_date(struct timespec *ts) { - struct timespec tsp; - if (!ts) { - clock_gettime(CLOCK_REALTIME, &tsp); - ts = &tsp; - } - // Example: 2015-09-21T11:42:25+02:00 char date[64]; - strftimespec(date, sizeof(date), "%FT%T%z", ts); + strftimespec(date, sizeof(date), "%FT%T.%u%z", ts); return json_string(date); } @@ -193,10 +187,10 @@ void ngsi_prepare_context(struct node *n, config_setting_t *mapping) "type", "integer", "value", j )); - json_array_append(metadatas, json_pack("{ s: s, s: s, s: o }", + json_array_append(metadatas, json_pack("{ s: s, s: s, s: s }", "name", "timestamp", "type", "date", - "value", json_date(NULL) + "value", "" )); if (i->structure == NGSI_CHILDREN) { diff --git a/server/src/utils.c b/server/src/utils.c index aab070f58..d0fe310b2 100644 --- a/server/src/utils.c +++ b/server/src/utils.c @@ -31,14 +31,33 @@ int version_compare(struct version *a, struct version *b) { return major ? major : minor; } -int strftimespec(char *s, uint max, const char *format, struct timespec *ts) +int strftimespec(char *dst, size_t max, const char *fmt, struct timespec *ts) { - struct tm t; + int s, d; + char fmt2[64], dst2[64]; - if (localtime_r(&(ts->tv_sec), &t) == NULL) - return -1; + for (s=0, d=0; fmt[s] && d < 64;) { + char c = fmt2[d++] = fmt[s++]; + if (c == '%') { + char n = fmt[s]; + if (n == 'u') { + fmt2[d++] = '0'; + fmt2[d++] = '3'; + } + else + fmt2[d++] = '%'; + } + } + + /* Print sub-second part to format string */ + snprintf(dst2, sizeof(dst2), fmt2, ts->tv_nsec / 1000000); - return strftime(s, max, format, &t); + /* Print timestamp */ + struct tm tm; + if (localtime_r(&(ts->tv_sec), &tm) == NULL) + return -1; + + return strftime(dst, max, dst2, &tm); } double box_muller(float m, float s)