diff options
| author | Donald Sharp <sharpd@cumulusnetworks.com> | 2020-03-17 06:47:15 -0400 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-17 06:47:15 -0400 | 
| commit | 218326d04a05c6eb7575edc7169782457095d936 (patch) | |
| tree | be5e8040f6f8b9f36a5b7979331702b8a7dcc013 | |
| parent | fc74cbe76cf198b325cfb93e8b161d861416086a (diff) | |
| parent | d0636ead31ccfc9f6c27d02617d5965e3111d4ad (diff) | |
Merge pull request #5927 from mjstapp/interval_string_api
lib, *: add a common time interval formatting api
| -rw-r--r-- | isisd/isis_misc.c | 18 | ||||
| -rw-r--r-- | isisd/isis_vty_fabricd.c | 16 | ||||
| -rw-r--r-- | lib/monotime.h | 20 | ||||
| -rw-r--r-- | ripd/rip_peer.c | 15 | ||||
| -rw-r--r-- | ripngd/ripng_peer.c | 15 | ||||
| -rw-r--r-- | zebra/zebra_vty.c | 45 | ||||
| -rw-r--r-- | zebra/zserv.c | 42 | 
7 files changed, 50 insertions, 121 deletions
diff --git a/isisd/isis_misc.c b/isisd/isis_misc.c index 5fa33f5500..96b76da92d 100644 --- a/isisd/isis_misc.c +++ b/isisd/isis_misc.c @@ -562,20 +562,12 @@ void vty_multiline(struct vty *vty, const char *prefix, const char *format, ...)  void vty_out_timestr(struct vty *vty, time_t uptime)  { -	struct tm tm;  	time_t difftime = time(NULL); +	char buf[MONOTIME_STRLEN]; +  	difftime -= uptime; -	gmtime_r(&difftime, &tm); - -	if (difftime < ONE_DAY_SECOND) -		vty_out(vty, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min, -			tm.tm_sec); -	else if (difftime < ONE_WEEK_SECOND) -		vty_out(vty, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour, -			tm.tm_min); -	else -		vty_out(vty, "%02dw%dd%02dh", tm.tm_yday / 7, -			tm.tm_yday - ((tm.tm_yday / 7) * 7), tm.tm_hour); -	vty_out(vty, " ago"); +	frrtime_to_interval(difftime, buf, sizeof(buf)); + +	vty_out(vty, "%s ago", buf);  } diff --git a/isisd/isis_vty_fabricd.c b/isisd/isis_vty_fabricd.c index 88f7337a91..09b8d28258 100644 --- a/isisd/isis_vty_fabricd.c +++ b/isisd/isis_vty_fabricd.c @@ -115,6 +115,7 @@ DEFUN (no_triggered_csnp,  static void lsp_print_flooding(struct vty *vty, struct isis_lsp *lsp)  {  	char lspid[255]; +	char buf[MONOTIME_STRLEN];  	lspid_print(lsp->hdr.lsp_id, lspid, true, true);  	vty_out(vty, "Flooding information for %s\n", lspid); @@ -129,21 +130,10 @@ static void lsp_print_flooding(struct vty *vty, struct isis_lsp *lsp)  		lsp->flooding_interface : "(null)");  	time_t uptime = time(NULL) - lsp->flooding_time; -	struct tm tm; -	gmtime_r(&uptime, &tm); +	frrtime_to_interval(uptime, buf, sizeof(buf)); -	if (uptime < ONE_DAY_SECOND) -		vty_out(vty, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min, -			tm.tm_sec); -	else if (uptime < ONE_WEEK_SECOND) -		vty_out(vty, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour, -			tm.tm_min); -	else -		vty_out(vty, "%02dw%dd%02dh", tm.tm_yday / 7, -			tm.tm_yday - ((tm.tm_yday / 7) * 7), -			tm.tm_hour); -	vty_out(vty, " ago)\n"); +	vty_out(vty, "%s ago)\n", buf);  	if (lsp->flooding_circuit_scoped) {  		vty_out(vty, "    Received as circuit-scoped LSP, so not " diff --git a/lib/monotime.h b/lib/monotime.h index e246f177de..dda763784f 100644 --- a/lib/monotime.h +++ b/lib/monotime.h @@ -112,6 +112,26 @@ static inline char *time_to_string(time_t ts, char *buf)  	return ctime_r(&tbuf, buf);  } +/* Convert interval to human-friendly string, used in cli output e.g. */ +static inline const char *frrtime_to_interval(time_t t, char *buf, +					      size_t buflen) +{ +	struct tm tm; + +	gmtime_r(&t, &tm); + +	if (t < ONE_DAY_SECOND) +		snprintf(buf, buflen, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min, +			 tm.tm_sec); +	else if (t < ONE_WEEK_SECOND) +		snprintf(buf, buflen, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour, +			 tm.tm_min); +	else +		snprintf(buf, buflen, "%02dw%dd%02dh", tm.tm_yday / 7, +			 tm.tm_yday - ((tm.tm_yday / 7) * 7), tm.tm_hour); +	return buf; +} +  #ifdef __cplusplus  }  #endif diff --git a/ripd/rip_peer.c b/ripd/rip_peer.c index 55dafd7c1f..77c73ab398 100644 --- a/ripd/rip_peer.c +++ b/ripd/rip_peer.c @@ -131,7 +131,6 @@ void rip_peer_bad_packet(struct rip *rip, struct sockaddr_in *from)  static char *rip_peer_uptime(struct rip_peer *peer, char *buf, size_t len)  {  	time_t uptime; -	struct tm tm;  	/* If there is no connection has been done before print `never'. */  	if (peer->uptime == 0) { @@ -142,17 +141,9 @@ static char *rip_peer_uptime(struct rip_peer *peer, char *buf, size_t len)  	/* Get current time. */  	uptime = time(NULL);  	uptime -= peer->uptime; -	gmtime_r(&uptime, &tm); - -	if (uptime < ONE_DAY_SECOND) -		snprintf(buf, len, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min, -			 tm.tm_sec); -	else if (uptime < ONE_WEEK_SECOND) -		snprintf(buf, len, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour, -			 tm.tm_min); -	else -		snprintf(buf, len, "%02dw%dd%02dh", tm.tm_yday / 7, -			 tm.tm_yday - ((tm.tm_yday / 7) * 7), tm.tm_hour); + +	frrtime_to_interval(uptime, buf, len); +  	return buf;  } diff --git a/ripngd/ripng_peer.c b/ripngd/ripng_peer.c index c038bfccf0..e6ff58dd0c 100644 --- a/ripngd/ripng_peer.c +++ b/ripngd/ripng_peer.c @@ -141,7 +141,6 @@ void ripng_peer_bad_packet(struct ripng *ripng, struct sockaddr_in6 *from)  static char *ripng_peer_uptime(struct ripng_peer *peer, char *buf, size_t len)  {  	time_t uptime; -	struct tm tm;  	/* If there is no connection has been done before print `never'. */  	if (peer->uptime == 0) { @@ -152,17 +151,9 @@ static char *ripng_peer_uptime(struct ripng_peer *peer, char *buf, size_t len)  	/* Get current time. */  	uptime = time(NULL);  	uptime -= peer->uptime; -	gmtime_r(&uptime, &tm); - -	if (uptime < ONE_DAY_SECOND) -		snprintf(buf, len, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min, -			 tm.tm_sec); -	else if (uptime < ONE_WEEK_SECOND) -		snprintf(buf, len, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour, -			 tm.tm_min); -	else -		snprintf(buf, len, "%02dw%dd%02dh", tm.tm_yday / 7, -			 tm.tm_yday - ((tm.tm_yday / 7) * 7), tm.tm_hour); + +	frrtime_to_interval(uptime, buf, len); +  	return buf;  } diff --git a/zebra/zebra_vty.c b/zebra/zebra_vty.c index 79ce43be93..ccc6e9e46b 100644 --- a/zebra/zebra_vty.c +++ b/zebra/zebra_vty.c @@ -241,25 +241,13 @@ static void vty_show_ip_route_detail(struct vty *vty, struct route_node *rn,  		vty_out(vty, "\n");  		time_t uptime; -		struct tm tm;  		uptime = monotime(NULL);  		uptime -= re->uptime; -		gmtime_r(&uptime, &tm); -		vty_out(vty, "  Last update "); +		frrtime_to_interval(uptime, buf, sizeof(buf)); -		if (uptime < ONE_DAY_SECOND) -			vty_out(vty, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min, -				tm.tm_sec); -		else if (uptime < ONE_WEEK_SECOND) -			vty_out(vty, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour, -				tm.tm_min); -		else -			vty_out(vty, "%02dw%dd%02dh", tm.tm_yday / 7, -				tm.tm_yday - ((tm.tm_yday / 7) * 7), -				tm.tm_hour); -		vty_out(vty, " ago\n"); +		vty_out(vty, "  Last update %s ago\n", buf);  		if (show_ng)  			vty_out(vty, "  Nexthop Group ID: %u\n", re->nhe_id); @@ -402,14 +390,15 @@ static void vty_show_ip_route(struct vty *vty, struct route_node *rn,  	json_object *json_route = NULL;  	json_object *json_labels = NULL;  	time_t uptime; -	struct tm tm;  	struct vrf *vrf = NULL;  	rib_dest_t *dest = rib_dest_from_rnode(rn);  	struct nexthop_group *nhg; +	char up_str[MONOTIME_STRLEN];  	uptime = monotime(NULL);  	uptime -= re->uptime; -	gmtime_r(&uptime, &tm); + +	frrtime_to_interval(uptime, up_str, sizeof(up_str));  	/* If showing fib information, use the fib view of the  	 * nexthops. @@ -474,18 +463,8 @@ static void vty_show_ip_route(struct vty *vty, struct route_node *rn,  		json_object_int_add(json_route, "internalNextHopActiveNum",  				    nexthop_group_active_nexthop_num(  					    &(re->nhe->nhg))); -		if (uptime < ONE_DAY_SECOND) -			sprintf(buf, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min, -				tm.tm_sec); -		else if (uptime < ONE_WEEK_SECOND) -			sprintf(buf, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour, -				tm.tm_min); -		else -			sprintf(buf, "%02dw%dd%02dh", tm.tm_yday / 7, -				tm.tm_yday - ((tm.tm_yday / 7) * 7), -				tm.tm_hour); -		json_object_string_add(json_route, "uptime", buf); +		json_object_string_add(json_route, "uptime", up_str);  		for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {  			json_nexthop = json_object_new_object(); @@ -774,17 +753,7 @@ static void vty_show_ip_route(struct vty *vty, struct route_node *rn,  					       sizeof(buf), 1));  		} -		if (uptime < ONE_DAY_SECOND) -			vty_out(vty, ", %02d:%02d:%02d", tm.tm_hour, -				tm.tm_min, tm.tm_sec); -		else if (uptime < ONE_WEEK_SECOND) -			vty_out(vty, ", %dd%02dh%02dm", tm.tm_yday, -				tm.tm_hour, tm.tm_min); -		else -			vty_out(vty, ", %02dw%dd%02dh", tm.tm_yday / 7, -				tm.tm_yday - ((tm.tm_yday / 7) * 7), -				tm.tm_hour); -		vty_out(vty, "\n"); +		vty_out(vty, ", %s\n", up_str);  	}  } diff --git a/zebra/zserv.c b/zebra/zserv.c index 40aa9010c5..7f806d82c3 100644 --- a/zebra/zserv.c +++ b/zebra/zserv.c @@ -858,7 +858,6 @@ void zserv_event(struct zserv *client, enum zserv_event event)  #define ZEBRA_TIME_BUF 32  static char *zserv_time_buf(time_t *time1, char *buf, int buflen)  { -	struct tm tm;  	time_t now;  	assert(buf != NULL); @@ -872,17 +871,9 @@ static char *zserv_time_buf(time_t *time1, char *buf, int buflen)  	now = monotime(NULL);  	now -= *time1; -	gmtime_r(&now, &tm); - -	if (now < ONE_DAY_SECOND) -		snprintf(buf, buflen, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min, -			 tm.tm_sec); -	else if (now < ONE_WEEK_SECOND) -		snprintf(buf, buflen, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour, -			 tm.tm_min); -	else -		snprintf(buf, buflen, "%02dw%dd%02dh", tm.tm_yday / 7, -			 tm.tm_yday - ((tm.tm_yday / 7) * 7), tm.tm_hour); + +	frrtime_to_interval(now, buf, buflen); +  	return buf;  } @@ -1001,8 +992,6 @@ static void zebra_show_stale_client_detail(struct vty *vty,  					   struct zserv *client)  {  	char buf[PREFIX2STR_BUFFER]; -	struct tm tm; -	struct timeval tv;  	time_t uptime;  	struct client_gr_info *info = NULL;  	struct zserv *s = NULL; @@ -1028,26 +1017,13 @@ static void zebra_show_stale_client_detail(struct vty *vty,  		if (ZEBRA_CLIENT_GR_ENABLED(info->capabilities)) {  			if (info->stale_client_ptr) {  				s = (struct zserv *)(info->stale_client_ptr); -				uptime = monotime(&tv); +				uptime = monotime(NULL);  				uptime -= s->restart_time; -				gmtime_r(&uptime, &tm); - -				vty_out(vty, "Last restart time : "); -				if (uptime < ONE_DAY_SECOND) -					vty_out(vty, "%02d:%02d:%02d", -						tm.tm_hour, tm.tm_min, -						tm.tm_sec); -				else if (uptime < ONE_WEEK_SECOND) -					vty_out(vty, "%dd%02dh%02dm", -						tm.tm_yday, tm.tm_hour, -						tm.tm_min); -				else -					vty_out(vty, "%02dw%dd%02dh", -						tm.tm_yday / 7, -						tm.tm_yday - ((tm.tm_yday / 7) -							       * 7), -						tm.tm_hour); -				vty_out(vty, "  ago\n"); + +				frrtime_to_interval(uptime, buf, sizeof(buf)); + +				vty_out(vty, "Last restart time : %s ago\n", +					buf);  				vty_out(vty, "Stalepath removal time: %d sec\n",  					info->stale_removal_time);  | 
