diff options
author | Rodrigo Ramírez Norambuena <a@rodrigoramirez.com> | 2016-02-18 01:58:01 -0300 |
---|---|---|
committer | Rodrigo Ramírez Norambuena <a@rodrigoramirez.com> | 2016-03-07 03:42:18 -0300 |
commit | 0ec9fe54219f8ad8a1e2c6f0a17843dcf08b7451 (patch) | |
tree | c2607814027ff8a7c5c9c88b49542948003ec819 /res | |
parent | 62282bb8ce15e3e70b3e2fd79834c02911b5f8ff (diff) |
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
Diffstat (limited to 'res')
-rw-r--r-- | res/res_config_ldap.c | 25 | ||||
-rw-r--r-- | res/res_config_pgsql.c | 24 |
2 files changed, 11 insertions, 38 deletions
diff --git a/res/res_config_ldap.c b/res/res_config_ldap.c index 95eae29c1..845427379 100644 --- a/res/res_config_ldap.c +++ b/res/res_config_ldap.c @@ -1837,7 +1837,9 @@ static int ldap_reconnect(void) */ static char *realtime_ldap_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) { - char status[256], credentials[100] = ""; + char status[256]; + char credentials[100] = ""; + char buf[362]; /* 256+100+" for "+NULL */ int ctimesec = time(NULL) - connect_time; switch (cmd) { @@ -1860,25 +1862,8 @@ static char *realtime_ldap_status(struct ast_cli_entry *e, int cmd, struct ast_c if (!ast_strlen_zero(user)) snprintf(credentials, sizeof(credentials), " with username %s", user); - if (ctimesec > 31536000) { - ast_cli(a->fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n", - status, credentials, ctimesec / 31536000, - (ctimesec % 31536000) / 86400, (ctimesec % 86400) / 3600, - (ctimesec % 3600) / 60, ctimesec % 60); - } else if (ctimesec > 86400) { - ast_cli(a->fd, "%s%s for %d days, %d hours, %d minutes, %d seconds.\n", - status, credentials, ctimesec / 86400, (ctimesec % 86400) / 3600, - (ctimesec % 3600) / 60, ctimesec % 60); - } else if (ctimesec > 3600) { - ast_cli(a->fd, "%s%s for %d hours, %d minutes, %d seconds.\n", - status, credentials, ctimesec / 3600, (ctimesec % 3600) / 60, - ctimesec % 60); - } else if (ctimesec > 60) { - ast_cli(a->fd, "%s%s for %d minutes, %d seconds.\n", status, credentials, - ctimesec / 60, ctimesec % 60); - } else { - ast_cli(a->fd, "%s%s for %d seconds.\n", status, credentials, ctimesec); - } + snprintf(buf, sizeof(buf), "%s%s for ", status, credentials); + ast_cli_print_timestr_fromseconds(a->fd, ctimesec, buf); return CLI_SUCCESS; } 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); |