From b37f5f53b3ef37af4c70ca358d3e74ce73fdabd1 Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Fri, 20 Dec 2024 17:13:19 +0200 Subject: [PATCH] bgpd: Replace ctime_r() to time_to_string[_json]() to handle JSON/non-JSON For JSON output we don't need newline to be printed. Before: ``` "lastUpdate":{"epoch":1734490463,"string":"Wed Dec 18 04:54:23 2024\n" ``` After: ``` "lastUpdate":{"epoch":1734678560,"string":"Fri Dec 20 09:09:20 2024" ``` Signed-off-by: Donatas Abraitis --- bgpd/bgp_labelpool.c | 4 +--- bgpd/bgp_mplsvpn.c | 4 +--- bgpd/bgp_nexthop.c | 4 ++-- bgpd/bgp_route.c | 4 ++-- bgpd/bgp_updgrp.c | 4 ++-- 5 files changed, 8 insertions(+), 12 deletions(-) diff --git a/bgpd/bgp_labelpool.c b/bgpd/bgp_labelpool.c index 23e0c191dc..54a966e191 100644 --- a/bgpd/bgp_labelpool.c +++ b/bgpd/bgp_labelpool.c @@ -1125,7 +1125,6 @@ static void show_bgp_nexthop_label_afi(struct vty *vty, afi_t afi, struct bgp_path_info *path; struct bgp *bgp_path; struct bgp_table *table; - time_t tbuf; vty_out(vty, "Current BGP label nexthop cache for %s, VRF %s\n", afi2str(afi), bgp->name_pretty); @@ -1146,8 +1145,7 @@ static void show_bgp_nexthop_label_afi(struct vty *vty, afi_t afi, vty_out(vty, " if %s\n", ifindex2ifname(iter->nh->ifindex, iter->nh->vrf_id)); - tbuf = time(NULL) - (monotime(NULL) - iter->last_update); - vty_out(vty, " Last update: %s", ctime_r(&tbuf, buf)); + vty_out(vty, " Last update: %s", time_to_string(iter->last_update, buf)); if (!detail) continue; vty_out(vty, " Paths:\n"); diff --git a/bgpd/bgp_mplsvpn.c b/bgpd/bgp_mplsvpn.c index ecb78c1ce4..85cc8f3596 100644 --- a/bgpd/bgp_mplsvpn.c +++ b/bgpd/bgp_mplsvpn.c @@ -4395,7 +4395,6 @@ static void show_bgp_mplsvpn_nh_label_bind_internal(struct vty *vty, struct bgp_path_info *path; struct bgp *bgp_path; struct bgp_table *table; - time_t tbuf; char buf[32]; vty_out(vty, "Current BGP mpls-vpn nexthop label bind cache, %s\n", @@ -4413,8 +4412,7 @@ static void show_bgp_mplsvpn_nh_label_bind_internal(struct vty *vty, vty_out(vty, " interface %s\n", ifindex2ifname(iter->nh->ifindex, iter->nh->vrf_id)); - tbuf = time(NULL) - (monotime(NULL) - iter->last_update); - vty_out(vty, " Last update: %s", ctime_r(&tbuf, buf)); + vty_out(vty, " Last update: %s", time_to_string(iter->last_update, buf)); if (!detail) continue; vty_out(vty, " Paths:\n"); diff --git a/bgpd/bgp_nexthop.c b/bgpd/bgp_nexthop.c index 1ef90a8e38..5fda5701f3 100644 --- a/bgpd/bgp_nexthop.c +++ b/bgpd/bgp_nexthop.c @@ -1079,14 +1079,14 @@ static void bgp_show_nexthop(struct vty *vty, struct bgp *bgp, json_last_update = json_object_new_object(); json_object_int_add(json_last_update, "epoch", tbuf); json_object_string_add(json_last_update, "string", - ctime_r(&tbuf, timebuf)); + time_to_string_json(bnc->last_update, timebuf)); json_object_object_add(json_nexthop, "lastUpdate", json_last_update); } else { json_object_int_add(json_nexthop, "lastUpdate", tbuf); } } else { - vty_out(vty, " Last update: %s", ctime_r(&tbuf, timebuf)); + vty_out(vty, " Last update: %s", time_to_string(bnc->last_update, timebuf)); } /* show paths dependent on nexthop, if needed. */ diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c index e142d96618..3399c8a2d0 100644 --- a/bgpd/bgp_route.c +++ b/bgpd/bgp_route.c @@ -11517,11 +11517,11 @@ void route_vty_out_detail(struct vty *vty, struct bgp *bgp, struct bgp_dest *bn, json_last_update = json_object_new_object(); json_object_int_add(json_last_update, "epoch", tbuf); json_object_string_add(json_last_update, "string", - ctime_r(&tbuf, timebuf)); + time_to_string_json(path->uptime, timebuf)); json_object_object_add(json_path, "lastUpdate", json_last_update); } else - vty_out(vty, " Last update: %s", ctime_r(&tbuf, timebuf)); + vty_out(vty, " Last update: %s", time_to_string(path->uptime, timebuf)); /* Line 10 display PMSI tunnel attribute, if present */ if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL))) { diff --git a/bgpd/bgp_updgrp.c b/bgpd/bgp_updgrp.c index ef03606707..4d05187616 100644 --- a/bgpd/bgp_updgrp.c +++ b/bgpd/bgp_updgrp.c @@ -757,7 +757,7 @@ static int update_group_show_walkcb(struct update_group *updgrp, void *arg) json_time = json_object_new_object(); json_object_int_add(json_time, "epoch", epoch_tbuf); json_object_string_add(json_time, "epochString", - ctime_r(&epoch_tbuf, timebuf)); + time_to_string_json(updgrp->uptime, timebuf)); json_object_object_add(json_updgrp, "groupCreateTime", json_time); json_object_string_add(json_updgrp, "afi", @@ -835,7 +835,7 @@ static int update_group_show_walkcb(struct update_group *updgrp, void *arg) json_object_int_add(json_subgrp_time, "epoch", epoch_tbuf); json_object_string_add(json_subgrp_time, "epochString", - ctime_r(&epoch_tbuf, timebuf)); + time_to_string_json(subgrp->uptime, timebuf)); json_object_object_add(json_subgrp, "groupCreateTime", json_subgrp_time); } else {