From 0ec9fe54219f8ad8a1e2c6f0a17843dcf08b7451 Mon Sep 17 00:00:00 2001 From: Rodrigo Ramírez Norambuena Date: Thu, 18 Feb 2016 01:58:01 -0300 Subject: main/cli.c: Refactor function to print seconds formatted Refactor and created function ast_cli_print_timestr_fromseconds to print seconds formatted: year(s) week(s) day(s) hour(s) second(s) This function now is used in addons/cdr_mysql.c,cdr_pgsql.c, main/cli.c, res_config_ldap.c, res_config_pgsql.c. Change-Id: Ibeb8634102cd11d3f8623398b279cb731bcde36c --- res/res_config_pgsql.c | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) (limited to 'res/res_config_pgsql.c') diff --git a/res/res_config_pgsql.c b/res/res_config_pgsql.c index 77c52aa0b..73f43ee2c 100644 --- a/res/res_config_pgsql.c +++ b/res/res_config_pgsql.c @@ -1574,7 +1574,9 @@ static char *handle_cli_realtime_pgsql_cache(struct ast_cli_entry *e, int cmd, s static char *handle_cli_realtime_pgsql_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) { - char connection_info[256], credentials[100] = ""; + char connection_info[256]; + char credentials[100] = ""; + char buf[376]; /* 256+100+"Connected to "+" for "+NULL */ int ctimesec = time(NULL) - connect_time; switch (cmd) { @@ -1601,24 +1603,10 @@ static char *handle_cli_realtime_pgsql_status(struct ast_cli_entry *e, int cmd, if (!ast_strlen_zero(dbuser)) snprintf(credentials, sizeof(credentials), " with username %s", dbuser); - if (pgsqlConn && PQstatus(pgsqlConn) == CONNECTION_OK) { - if (ctimesec > 31536000) - ast_cli(a->fd, "Connected to %s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n", - connection_info, credentials, ctimesec / 31536000, (ctimesec % 31536000) / 86400, - (ctimesec % 86400) / 3600, (ctimesec % 3600) / 60, ctimesec % 60); - else if (ctimesec > 86400) - ast_cli(a->fd, "Connected to %s%s for %d days, %d hours, %d minutes, %d seconds.\n", connection_info, - credentials, ctimesec / 86400, (ctimesec % 86400) / 3600, (ctimesec % 3600) / 60, - ctimesec % 60); - else if (ctimesec > 3600) - ast_cli(a->fd, "Connected to %s%s for %d hours, %d minutes, %d seconds.\n", connection_info, credentials, - ctimesec / 3600, (ctimesec % 3600) / 60, ctimesec % 60); - else if (ctimesec > 60) - ast_cli(a->fd, "Connected to %s%s for %d minutes, %d seconds.\n", connection_info, credentials, ctimesec / 60, - ctimesec % 60); - else - ast_cli(a->fd, "Connected to %s%s for %d seconds.\n", connection_info, credentials, ctimesec); + if (pgsqlConn && PQstatus(pgsqlConn) == CONNECTION_OK) { + snprintf(buf, sizeof(buf), "Connected to %s%s for ", connection_info, credentials); + ast_cli_print_timestr_fromseconds(a->fd, ctimesec, buf); return CLI_SUCCESS; } else { ast_cli(a->fd, "Unable to connect %s%s\n", connection_info, credentials); -- cgit v1.2.3