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 <donatas@opensourcerouting.org>
This commit is contained in:
Donatas Abraitis 2024-12-20 17:13:19 +02:00
parent b3e1ceaf88
commit b37f5f53b3
5 changed files with 8 additions and 12 deletions

View File

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

View File

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

View File

@ -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. */

View File

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

View File

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