From c71a88d67bfa7f3c7fe2d4a684057f364027e1ec 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 start time current connection:

Add extra information for \conninfo psql command to show the start time
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 93953fc8e7..1758e20ad7 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -263,6 +263,19 @@ HandleSlashCmds(PsqlScanState scan_state,
 }
 
 
+static void
+printTimeConnected(void)
+{
+
+	const char *strftime_fmt;
+	char timebuf[128];
+
+	strftime_fmt = "%c";
+	strftime(timebuf, sizeof(timebuf), strftime_fmt, localtime(&pset.connection_start));
+
+	printf(_("The connection started at %s\n"), timebuf);
+}
+
 /*
  * Subroutine to actually try to execute a backslash command.
  *
@@ -624,6 +637,7 @@ exec_command_conninfo(PsqlScanState scan_state, bool active_branch)
 			}
 			printSSLInfo();
 			printGSSInfo();
+			printTimeConnected();
 		}
 	}
 
@@ -3326,6 +3340,7 @@ SyncVariables(void)
 	pset.encoding = PQclientEncoding(pset.db);
 	pset.popt.topt.encoding = pset.encoding;
 	pset.sversion = PQserverVersion(pset.db);
+	pset.connection_start = 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 5be5091f0e..955e696d8e 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 */
+	time_t connection_start;	/* unixtime for start time connection */
 } PsqlSettings;
 
 extern PsqlSettings pset;
-- 
2.21.0

