On Wed, Sep 4, 2019 at 11:04 AM Alvaro Herrera <alvhe...@2ndquadrant.com> wrote: > > The only thing that seems wrong about this proposal is that the time > format is a bit too verbose. I would have it do "N days 12:23:34". >
Hi Alvaro!, I attach a second version with this change. -- Rodrigo Ramírez Norambuena http://www.rodrigoramirez.com/
From 3526b58e836f92da6c5da6b105c6748948b99365 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Ram=C3=ADrez=20Norambuena?= <a...@rodrigoramirez.com> Date: Tue, 3 Sep 2019 17:15:39 -0400 Subject: [PATCH] Add to the \conninfo for time of current connection: Add extra information for \conninfo psql command to show the current time lapsed of the connection. The time will be reestablished again if the connection is reset and reconnected successfully with the backend. diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index c0a7a5566e..8b0e87211d 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -263,6 +263,42 @@ HandleSlashCmds(PsqlScanState scan_state, } +static void +printTimeConnected(void) +{ + + int days = 0, hours = 0 , minutes = 0 , seconds = 0; + int secs = time(NULL) - pset.connected; + + #define SECOND (1) + #define MINUTE (SECOND * 60) + #define HOUR (MINUTE * 60) + #define DAY (HOUR * 24) + + if (secs > DAY) { + days = (secs / DAY); + secs -= (days * DAY); + } + if (secs > HOUR) { + hours = (secs / HOUR); + secs -= (hours * HOUR); + } + if (secs > MINUTE) { + minutes = (secs / MINUTE); + secs -= (minutes * MINUTE); + } + if (secs > 0) + seconds = secs; + + if (days > 0) + printf(ngettext("The time connection is %d day %02d:%02d:%02d\n", + "The time connection is %d days %02d:%02d:%02d\n", days), + days, hours, minutes, seconds); + else + printf(_("The time connection is %02d:%02d:%02d\n"), + hours, minutes, seconds); +} + /* * Subroutine to actually try to execute a backslash command. * @@ -624,6 +660,7 @@ exec_command_conninfo(PsqlScanState scan_state, bool active_branch) } printSSLInfo(); printGSSInfo(); + printTimeConnected(); } } @@ -3318,6 +3355,7 @@ SyncVariables(void) pset.encoding = PQclientEncoding(pset.db); pset.popt.topt.encoding = pset.encoding; pset.sversion = PQserverVersion(pset.db); + pset.connected = time(NULL); SetVariable(pset.vars, "DBNAME", PQdb(pset.db)); SetVariable(pset.vars, "USER", PQuser(pset.db)); diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 44a782478d..c9d4eaf0e7 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -408,7 +408,10 @@ CheckConnection(void) UnsyncVariables(); } else + { + pset.connected = time(NULL); fprintf(stderr, _("Succeeded.\n")); + } } return OK; diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h index 5be5091f0e..ed3ab368f5 100644 --- a/src/bin/psql/settings.h +++ b/src/bin/psql/settings.h @@ -141,6 +141,7 @@ typedef struct _psqlSettings const char *prompt3; PGVerbosity verbosity; /* current error verbosity level */ PGContextVisibility show_context; /* current context display level */ + int connected; /* unixtime for connected init time */ } PsqlSettings; extern PsqlSettings pset; -- 2.21.0