From ebed26d8e90744b46d2903a8f0449f748cdbeb1a Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Wed, 16 Sep 2020 17:06:32 -0300
Subject: [PATCH v8 2/2] Remove use of obsolete ftime()

Loses output of milliseconds :-(
---
 src/interfaces/libpq/fe-misc.c | 47 ++++++----------------------------
 1 file changed, 8 insertions(+), 39 deletions(-)

diff --git a/src/interfaces/libpq/fe-misc.c b/src/interfaces/libpq/fe-misc.c
index de84da6e83..ee479d7863 100644
--- a/src/interfaces/libpq/fe-misc.c
+++ b/src/interfaces/libpq/fe-misc.c
@@ -35,7 +35,6 @@
 
 #ifdef WIN32
 #include "win32.h"
-#include <windows.h>
 #else
 #include <unistd.h>
 #include <sys/time.h>
@@ -46,7 +45,6 @@
 #endif
 #ifdef HAVE_SYS_SELECT_H
 #include <sys/select.h>
-#include <sys/timeb.h>
 #endif
 
 #include "libpq-fe.h"
@@ -159,8 +157,6 @@ static void pqLogMsgString(PGconn *conn, const char *v, int length,
 static void pqLogMsgnchar(PGconn *conn, const char *v, int length,
 						  PGCommSource commsource);
 static void pqLogMsgInt(PGconn *conn, int v, int length, PGCommSource commsource);
-static void pqGetCurrentTime(char *currenttime);
-#define	TRACELOG_TIME_SIZE	33
 
 /*
  * pqGetProtocolMsgType:
@@ -322,38 +318,6 @@ pqLogFrontendMsg(PGconn *conn)
 	pqInitMsgLog(conn);
 }
 
-/*
- * pqGetCurrentTime: get current time for trace log output
- */
-static void
-pqGetCurrentTime(char *currenttime)
-{
-#ifdef WIN32
-	SYSTEMTIME	localTime;
-	TIME_ZONE_INFORMATION TimezoneInfo;
-
-	GetLocalTime(&localTime);
-
-	GetTimeZoneInformation(&TimezoneInfo);
-	snprintf(currenttime, TRACELOG_TIME_SIZE, "%4d-%02d-%02d %02d:%02d:%02d.%03d %s ",
-			 localTime.wYear, localTime.wMonth, localTime.wDay,
-			 localTime.wHour, localTime.wMinute, localTime.wSecond,
-			 localTime.wMilliseconds, TimezoneInfo.Bias);
-#else
-	struct timeb localTime;
-	struct tm  *tm;
-	char		timezone[100];
-
-	ftime(&localTime);
-	tm = localtime(&localTime.time);
-
-	strftime(timezone, sizeof(timezone), "%Z", tm);
-	snprintf(currenttime, TRACELOG_TIME_SIZE, "%4d-%02d-%02d %02d:%02d:%02d.%03d %s ",
-			 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday,
-			 tm->tm_hour, tm->tm_min, tm->tm_sec, localTime.millitm, timezone);
-#endif
-}
-
 /*
  * PQlibVersion: return the libpq version number
  */
@@ -408,15 +372,20 @@ pqLogMsgByte1(PGconn *conn, char v, PGCommSource commsource)
 {
 	const char *protocol_message_type;
 	char	   *message_source = commsource == FROM_BACKEND ? "<" : ">";
-	char		current_time[TRACELOG_TIME_SIZE];
+	time_t		currtime;
+	struct tm  *tmp;
+	char		timestr[128];
 
 	if (PG_PROTOCOL_MAJOR(conn->pversion) >= 3)
 	{
 		switch (conn->logging_message.state)
 		{
 			case LOG_FIRST_BYTE:
-				pqGetCurrentTime(current_time);
-				fprintf(conn->Pfdebug, "%s %s ", current_time, message_source);
+				currtime = time(NULL);
+				tmp = localtime(&currtime);
+				strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S %Z", tmp);
+
+				fprintf(conn->Pfdebug, "%s %s ", timestr, message_source);
 				/* If there is no first 1 byte protocol message, */
 				if (v == ' ')
 					return;
-- 
2.20.1

