From 76825bcc207d388768161746a3bbf8c2e7da9e34 Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Thu, 13 Dec 2018 21:59:21 +0100 Subject: [PATCH] Display server's current date time instead of useless UTC time in status bar --- source/dbconnection.pas | 25 ++++++++++++++++++++++--- source/main.pas | 27 +++++++++++++-------------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/source/dbconnection.pas b/source/dbconnection.pas index a5d7eadb..f07147a0 100644 --- a/source/dbconnection.pas +++ b/source/dbconnection.pas @@ -297,8 +297,9 @@ type TDBConnection = class(TComponent) private FActive: Boolean; - FConnectionStarted: Integer; + FConnectionStarted: Cardinal; FServerUptime: Integer; + FServerDateTimeOnStartup: String; FParameters: TConnectionParameters; FLoginPromptDone: Boolean; FDatabase: String; @@ -356,6 +357,7 @@ type function GetInformationSchemaObjects: TStringList; virtual; function GetConnectionUptime: Integer; function GetServerUptime: Integer; + function GetServerNow: TDateTime; function GetCurrentUserHostCombination: String; function DecodeAPIString(a: AnsiString): String; function ExtractIdentifier(var SQL: String): String; @@ -413,6 +415,7 @@ type property ThreadId: Int64 read GetThreadId; property ConnectionUptime: Integer read GetConnectionUptime; property ServerUptime: Integer read GetServerUptime; + property ServerNow: TDateTime read GetServerNow; property CharacterSet: String read GetCharacterSet write SetCharacterSet; property LastErrorCode: Cardinal read GetLastErrorCode; property LastError: String read GetLastError; @@ -1823,6 +1826,7 @@ begin FIsSSL := Status.Col(1) <> ''; Status.Next; end; + FServerDateTimeOnStartup := GetVar('SELECT NOW()'); FServerVersionUntouched := DecodeAPIString(mysql_get_server_info(FHandle)); Vars := GetSessionVariables(False); while not Vars.Eof do begin @@ -1958,6 +1962,7 @@ begin except FServerUptime := -1; end; + FServerDateTimeOnStartup := GetVar('SELECT GETDATE()'); // Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (Intel X86) // Apr 2 2010 15:53:02 // Copyright (c) Microsoft Corporation @@ -2053,6 +2058,7 @@ begin raise EDatabaseError.Create(Error); end; FActive := True; + FServerDateTimeOnStartup := GetVar('SELECT NOW()'); FServerVersionUntouched := GetVar('SELECT VERSION()'); FConnectionStarted := GetTickCount div 1000; Log(lcInfo, f_('Connected. Thread-ID: %d', [ThreadId])); @@ -4303,7 +4309,7 @@ begin if not FActive then Result := 0 else - Result := Integer(GetTickCount div 1000) - FConnectionStarted; + Result := (GetTickCount div 1000) - FConnectionStarted; end; @@ -4311,12 +4317,25 @@ function TDBConnection.GetServerUptime: Integer; begin // Return server uptime in seconds. Return -1 if unknown. if FServerUptime > 0 then - Result := FServerUptime + (Integer(GetTickCount div 1000) - FConnectionStarted) + Result := Cardinal(FServerUptime) + ((GetTickCount div 1000) - FConnectionStarted) else Result := -1; end; +function TDBConnection.GetServerNow: TDateTime; +var + d: TDateTime; +begin + // Return server datetime. Return -1 if unknown. + if not FServerDateTimeOnStartup.IsEmpty then begin + d := StrToDateTimeDef(FServerDateTimeOnStartup, 0); + Result := IncSecond(d, (GetTickCount div 1000) - FConnectionStarted); + end else + Result := -1; +end; + + function TDBConnection.GetCurrentUserHostCombination: String; begin // Return current user@host combination, used by various object editors for DEFINER clauses diff --git a/source/main.pas b/source/main.pas index c124ee8c..3d6f3b20 100644 --- a/source/main.pas +++ b/source/main.pas @@ -2243,8 +2243,8 @@ begin w1 := CalcPanelWidth(110, 10); w2 := CalcPanelWidth(140, 10); w3 := CalcPanelWidth(170, 15); - w4 := CalcPanelWidth(170, 15); - w5 := CalcPanelWidth(170, 15); + w4 := CalcPanelWidth(150, 15); + w5 := CalcPanelWidth(210, 15); w6 := CalcPanelWidth(250, 20); w0 := StatusBar.Width - w1 - w2 - w3 - w4 - w5 - w6; StatusBar.Panels[0].Width := w0; @@ -5875,11 +5875,9 @@ procedure TMainForm.TimerHostUptimeTimer(Sender: TObject); var Conn: TDBConnection; Uptime: Integer; - SystemTime: TSystemTime; - tmp: TDateTime; - utcs: String; + ServerNow: TDateTime; begin - // Display server uptime + // Display server uptime and current date time Conn := ActiveConnection; if Assigned(Conn) then begin Uptime := Conn.ServerUptime; @@ -5887,15 +5885,16 @@ begin ShowStatusMsg(_('Uptime')+': '+FormatTimeNumber(Conn.ServerUptime, False), 4) else ShowStatusMsg(_('Uptime')+': '+_('unknown'), 4); - end else - ShowStatusMsg('', 4); - // Display UTC date/time - GetSystemTime(SystemTime); - tmp := EncodeDate(SystemTime.wYear, SystemTime.wMonth, SystemTime.wDay) + - EncodeTime(SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond, SystemTime.wMilliseconds); - DateTimeToString(utcs, 'ddddd t', tmp); - ShowStatusMsg('UTC: ' + utcs, 5); + ServerNow := Conn.ServerNow; + if ServerNow >= 0 then + ShowStatusMsg(f_('Server time: %s', [DateTimeToStr(ServerNow)]), 5) + else + ShowStatusMsg(f_('Server time: %s', [_('unknown')]), 5); + end else begin + ShowStatusMsg('', 4); + end; + end;