Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save roramirez/d6c7a59affe43296144a9a262de665a0 to your computer and use it in GitHub Desktop.
Save roramirez/d6c7a59affe43296144a9a262de665a0 to your computer and use it in GitHub Desktop.
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 4fa7760..6ef6ed9 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -161,6 +161,55 @@ HandleSlashCmds(PsqlScanState scan_state,
return status;
}
+static void
+print_seconds_ago(int secs)
+{
+
+ int x;
+
+ #define SECOND (1)
+ #define MINUTE (SECOND * 60)
+ #define HOUR (MINUTE * 60)
+ #define DAY (HOUR * 24)
+ #define WEEK (DAY * 7)
+ #define YEAR (DAY * 365)
+ #define NEEDCOMMA(x) ((x) ? ", " : "") /* define if we need a comma */
+ #define ESS(x) ((x) == 1 ? "" : "s")
+
+
+ printf("For ");
+ if (secs > YEAR) {
+ x = (secs / YEAR);
+ secs -= (x * YEAR);
+ printf("%d year%s%s", x, ESS(x), NEEDCOMMA(secs));
+ }
+ if (secs > WEEK) {
+ x = (secs / WEEK);
+ secs -= (x * WEEK);
+ printf("%d week%s%s", x, ESS(x), NEEDCOMMA(secs));
+ }
+ if (secs > DAY) {
+ x = (secs / DAY);
+ secs -= (x * DAY);
+ printf("%d day%s%s", x, ESS(x), NEEDCOMMA(secs));
+ }
+ if (secs > HOUR) {
+ x = (secs / HOUR);
+ secs -= (x * HOUR);
+ printf("%d hour%s%s", x, ESS(x), NEEDCOMMA(secs));
+ }
+ if (secs > MINUTE) {
+ x = (secs / MINUTE);
+ secs -= (x * MINUTE);
+ printf("%d minute%s%s", x, ESS(x), NEEDCOMMA(secs));
+ }
+ x = secs;
+ if (x > 0) {
+ printf("%d second%s", x, ESS(x));
+ }
+ printf(".\n");
+}
+
/*
* Read and interpret an argument to the \connect slash command.
*/
@@ -340,12 +389,14 @@ exec_command(const char *cmd,
/* If the host is an absolute path, the connection is via socket */
if (is_absolute_path(host))
- printf(_("You are connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),
+ printf(_("You are connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\"\n"),
db, PQuser(pset.db), host, PQport(pset.db));
else
- printf(_("You are connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n"),
+ printf(_("You are connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\"\n"),
db, PQuser(pset.db), host, PQport(pset.db));
printSSLInfo();
+ if (pset.connected != NULL && (time(NULL) - pset.connected) > 0)
+ print_seconds_ago(time(NULL) - pset.connected);
PQconninfoFree(connOptions);
}
@@ -2063,6 +2114,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/settings.h b/src/bin/psql/settings.h
index 8cfe9d2..e2a70bb 100644
--- a/src/bin/psql/settings.h
+++ b/src/bin/psql/settings.h
@@ -135,6 +135,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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment