Skip to content

Commit b8a4291

Browse files
committed
statd: share the date-and-time formatter
The mDNS neighbor code has a private formatter for YANG date-and-time strings. More operational leaves need the same encoding, so give it a home in shared.c and let callers pass the time to format. Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
1 parent 9c58568 commit b8a4291

3 files changed

Lines changed: 15 additions & 10 deletions

File tree

‎src/statd/avahi.c‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <srx/common.h>
3737

3838
#include "avahi.h"
39+
#include "shared.h"
3940

4041
/* Complete the opaque avahi types declared in avahi-common/watch.h */
4142
struct AvahiWatch {
@@ -336,15 +337,6 @@ static void free_all(struct mdns_ctx *ctx)
336337

337338
#define XPATH_BASE "/infix-services:mdns/neighbors"
338339

339-
static void format_timestamp(char *buf, size_t sz)
340-
{
341-
struct tm tm;
342-
time_t now = time(NULL);
343-
344-
gmtime_r(&now, &tm);
345-
strftime(buf, sz, "%Y-%m-%dT%H:%M:%S+00:00", &tm);
346-
}
347-
348340
static int sr_setstr(sr_session_ctx_t *ses, const char *xpath, const char *val)
349341
{
350342
int err = sr_set_item_str(ses, xpath, val, NULL, 0);
@@ -449,7 +441,7 @@ static void ds_push_resolver(struct mdns_ctx *ctx, struct avahi_service *svc,
449441
}
450442

451443
/* last-seen */
452-
format_timestamp(ts, sizeof(ts));
444+
format_timestamp(time(NULL), ts, sizeof(ts));
453445
snprintf(xpath, sizeof(xpath),
454446
XPATH_BASE "/neighbor[hostname='%s']/last-seen", svc->hostname);
455447
err = err ?: sr_setstr(ctx->sr_ses, xpath, ts);

‎src/statd/shared.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <stdio.h>
44
#include <stdlib.h>
5+
#include <time.h>
56
#include <jansson.h>
67
#include <net/if.h>
78
#include <libite/lite.h>
@@ -76,3 +77,12 @@ int ip_link_check_group(const char *ifname, const char *group)
7677

7778
return 0;
7879
}
80+
81+
/* YANG date-and-time, always in UTC */
82+
void format_timestamp(time_t when, char *buf, size_t sz)
83+
{
84+
struct tm tm;
85+
86+
gmtime_r(&when, &tm);
87+
strftime(buf, sz, "%Y-%m-%dT%H:%M:%S+00:00", &tm);
88+
}

‎src/statd/shared.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
#ifndef STATD_SHARED_H_
44
#define STATD_SHARED_H_
55

6+
#include <stddef.h>
7+
#include <time.h>
68
#include <jansson.h>
79

810
json_t *json_get_output(const char *cmd);
911
int ip_link_check_group(const char *ifname, const char *group);
12+
void format_timestamp(time_t when, char *buf, size_t sz);
1013

1114
#endif

0 commit comments

Comments
 (0)