1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

added sub-second precision for json_date() ISO 8601

This commit is contained in:
Steffen Vogel 2015-10-12 13:45:05 +02:00
parent 4cf1b22783
commit 4271c17d68
3 changed files with 31 additions and 18 deletions

View file

@ -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)) \

View file

@ -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) {

View file

@ -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)