Skip to content

Commit

Permalink
IndySockets#333 Adding new LocalTimeToUTCTime() and UTCTimeToLocalTim…
Browse files Browse the repository at this point in the history
…e() functions in IdGlobal unit. Deprecating TimeZoneBias() function in IdGlobalProtocols unit, and GetLocalTime() function in IdSSLOpenSSLUtils unit. (IndySockets#355)
  • Loading branch information
rlebeau authored Jun 28, 2021
1 parent 6319951 commit a8f3ac5
Show file tree
Hide file tree
Showing 22 changed files with 121 additions and 123 deletions.
1 change: 1 addition & 0 deletions Lib/Core/IdCompilerDefines.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,7 @@
{$DEFINE HAS_PVOID}
{$DEFINE HAS_ULONG64}
{$DEFINE HAS_TEncoding_GetEncoding_ByEncodingName}
{$DEFINE HAS_DateUtils_TTimeZone}
{$IFDEF DCC}
// Exception.RaiseOuterException() is now available on all platforms
{$DEFINE HAS_Exception_RaiseOuterException}
Expand Down
1 change: 1 addition & 0 deletions Lib/FCL/IdCompilerDefines.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,7 @@
{$DEFINE HAS_PVOID}
{$DEFINE HAS_ULONG64}
{$DEFINE HAS_TEncoding_GetEncoding_ByEncodingName}
{$DEFINE HAS_DateUtils_TTimeZone}
{$IFDEF DCC}
// Exception.RaiseOuterException() is now available on all platforms
{$DEFINE HAS_Exception_RaiseOuterException}
Expand Down
1 change: 1 addition & 0 deletions Lib/Protocols/IdCompilerDefines.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,7 @@
{$DEFINE HAS_PVOID}
{$DEFINE HAS_ULONG64}
{$DEFINE HAS_TEncoding_GetEncoding_ByEncodingName}
{$DEFINE HAS_DateUtils_TTimeZone}
{$IFDEF DCC}
// Exception.RaiseOuterException() is now available on all platforms
{$DEFINE HAS_Exception_RaiseOuterException}
Expand Down
2 changes: 1 addition & 1 deletion Lib/Protocols/IdFSP.pas
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ procedure ParseStatInfo(const AData : TIdBytes; VL : TIdFSPStatInfo; var VI : UI
CopyBytesToHostUInt32(AData, VI, LC);

VL.FModifiedDateGMT := UnixDateTimeToDelphiDateTime(LC);
VL.FModifiedDate := VL.FModifiedDateGMT + OffSetFromUTC;
VL.FModifiedDate := UTCTimeToLocalTime(VL.FModifiedDateGMT);
Inc(VI, 4);

CopyBytesToHostUInt32(AData, VI, LC);
Expand Down
6 changes: 3 additions & 3 deletions Lib/Protocols/IdFTP.pas
Original file line number Diff line number Diff line change
Expand Up @@ -4024,7 +4024,7 @@ procedure TIdFTP.SetModTime(const AFileName: String; const ALocalTime: TDateTime
//syntax 3 - MDTM [local timestamp] Filename
else if FTZInfo.FGMTOffsetAvailable then begin
//send it relative to the server's time-zone
LCmd := 'MDTM '+ FTPDateTimeToMDTMD(ALocalTime - OffSetFromUTC + FTZInfo.FGMTOffset, False, False) + ' ' + AFileName; {do not localize}
LCmd := 'MDTM '+ FTPDateTimeToMDTMD(LocalTimeToUTCTime(ALocalTime) + FTZInfo.FGMTOffset, False, False) + ' ' + AFileName; {do not localize}
end

else begin
Expand Down Expand Up @@ -4067,7 +4067,7 @@ procedure TIdFTP.SetModTimeGMT(const AFileName : String; const AGMTTime: TDateTi
//Syntax 2 - MDTM yyyymmddhhmmss[+-minutes from Universal Time] Filename
//use old method for old versions of Serv-U and BPFTP Server
else if (IndexOfFeatLine('MDTM YYYYMMDDHHMMSS[+-TZ] filename') > 0) or IsOldServU or IsBPFTP then begin {do not localize}
LCmd := 'MDTM '+ FTPDateTimeToMDTMD(AGMTTime + OffSetFromUTC, False, True) + ' ' + AFileName; {do not localize}
LCmd := 'MDTM '+ FTPDateTimeToMDTMD(UTCTimeToLocalTime(AGMTTime), False, True) + ' ' + AFileName; {do not localize}
end

//syntax 3 - MDTM [local timestamp] Filename
Expand All @@ -4077,7 +4077,7 @@ procedure TIdFTP.SetModTimeGMT(const AFileName : String; const AGMTTime: TDateTi
end

else begin
LCmd := 'MDTM '+ FTPDateTimeToMDTMD(AGMTTime + OffSetFromUTC, False, False) + ' ' + AFileName; {do not localize}
LCmd := 'MDTM '+ FTPDateTimeToMDTMD(UTCTimeToLocalTime(AGMTTime), False, False) + ' ' + AFileName; {do not localize}
end;

// When using MDTM, Titan FTP 5 returns 200 and vsFTPd returns 213
Expand Down
25 changes: 22 additions & 3 deletions Lib/Protocols/IdFTPCommon.pas
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ implementation
Posix.SysTime,
Posix.Time,
{$ENDIF}
{$IFDEF HAS_DateUtils_TTimeZone}DateUtils,{$ENDIF}
IdException;

{WS_FTP Pro XAUT Support}
Expand Down Expand Up @@ -1219,7 +1220,7 @@ function EPLFDateToLocalDateTime(const AData: String): TDateTime;
LSecs : Int64;
begin
LSecs := IndyStrToInt(AData);
Result := Extended( ((LSecs)/ (24 * 60 * 60) ) + Int(BASE_DATE)) - IdGlobalProtocols.TimeZoneBias;
Result := UTCTimeToLocalTime( Extended( ((LSecs)/ (24 * 60 * 60) ) + Int(BASE_DATE)) );
end;

function EPLFDateToGMTDateTime(const AData: String): TDateTime;
Expand All @@ -1242,7 +1243,7 @@ function GMTDateTimeToEPLFDate(const ADateTime : TDateTime) : String;
function LocalDateTimeToEPLFDate(const ADateTime : TDateTime) : String;
{$IFDEF USE_INLINE} inline; {$ENDIF}
begin
Result := FloatToStr( Extended(ADateTime + IdGlobalProtocols.TimeZoneBias - Int(EPLF_BASE_DATE)) * 24 * 60 * 60);
Result := FloatToStr( Extended( LocalTimeToUTCTime(ADateTime) - Int(EPLF_BASE_DATE)) * 24 * 60 * 60);
end;

{Date routines}
Expand Down Expand Up @@ -1343,18 +1344,36 @@ function MDTMOffset(const AOffs : String) : TDateTime;
end;

function MinutesFromGMT : Integer;
{$IFDEF HAS_GetLocalTimeOffset}
{$IFDEF USE_INLINE} inline; {$ENDIF}
{$ELSE}
{$IFDEF HAS_DateUtils_TTimeZone}
{$IFDEF USE_INLINE} inline; {$ENDIF}
{$ELSE}
var
LD : TDateTime;
LHour, LMin, LSec, LMSec : Word;
{$ENDIF}
{$ENDIF}
begin
{$IFDEF HAS_GetLocalTimeOffset}
// RLebeau: Note that on Linux/Unix, this information may be inaccurate around
// the DST time changes (for optimization). In that case, the unix.ReReadLocalTime()
// function must be used to re-initialize the timezone information...
Result := {-1 *} GetLocalTimeOffset();
{$ELSE}
{$IFDEF HAS_DateUtils_TTimeZone}
Result := {-1 *} TTimeZone.Local.UtcOffset.TotalMinutes;
{$ELSE}
LD := OffsetFromUTC;
DecodeTime(LD, LHour, LMin, LSec, LMSec);
if LD < 0.0 then begin
Result := 0 - (LHour * 60 + LMin);
end else begin
Result := LHour * 60 + LMin;
end;
{$ENDIF}
{$ENDIF}
end;

function FTPDateTimeToMDTMD(const ATimeStamp : TDateTime; const AIncludeMSecs : Boolean=True; const AIncludeGMTOffset : Boolean=True): String;
Expand Down Expand Up @@ -1415,7 +1434,7 @@ function FTPMDTMToGMTDateTime(const ATimeStamp : String):TDateTime;
Result := EncodeDate(LYear, LMonth, LDay);
Result := Result + EncodeTime(LHour, LMin, LSec, LMSec);
if LOffset = '' then begin
Result := Result - OffsetFromUTC;
Result := LocalTimeToUTCTime(Result);
end else begin
Result := Result - MDTMOffset(LOffset);
end;
Expand Down
2 changes: 1 addition & 1 deletion Lib/Protocols/IdFTPListOutput.pas
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ function TIdFTPListOutput.GetItems(AIndex: Integer): TIdFTPListOutputItem;
function TIdFTPListOutput.GetLocalModTime(AItem: TIdFTPListOutputItem): TDateTime;
begin
if AItem.ModifiedDateGMT <> 0 then begin
Result := AItem.ModifiedDateGMT - TimeZoneBias;
Result := UTCTimeToLocalTime(AItem.ModifiedDateGMT);
end else begin
Result := AItem.ModifiedDate;
end;
Expand Down
2 changes: 1 addition & 1 deletion Lib/Protocols/IdFTPListParseDistinctTCPIP.pas
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class function TIdFTPLPDistinctTCPIP.ParseLine(const AItem: TIdFTPListItem;
// 12/29/2002 01:42p 23 CreateTest.txt
// I suspect that this server returns the timestamp as GMT
LI.ModifiedDateGMT := LI.ModifiedDate;
LI.ModifiedDate := LI.ModifiedDate - TimeZoneBias;
LI.ModifiedDate := UTCTimeToLocalTime(LI.ModifiedDateGMT);
// file name
LBuf := StripSpaces(LBuf, 1);
LI.FileName := LBuf;
Expand Down
10 changes: 6 additions & 4 deletions Lib/Protocols/IdFTPServer.pas
Original file line number Diff line number Diff line change
Expand Up @@ -5312,7 +5312,7 @@ procedure TIdFTPServer.DoOnGetFileDate(ASender: TIdFTPServerContext;
LFileSystem := FTPFileSystem;
if Assigned(LFileSystem) then begin
LFileSystem.GetFileDate(ASender, AFileName, VFileDate);
VFileDate := VFileDate - OffsetFromUTC;
VFileDate := LocalTimeToUTCTime(VFileDate);
end else if Assigned(FOnGetFileDate) then begin
FOnGetFileDate(ASender, AFileName, VFileDate);
end;
Expand Down Expand Up @@ -5477,7 +5477,7 @@ procedure TIdFTPServer.CommandSiteUTIME(ASender: TIdCommand);
if IsValidTimeStamp(ALSender.Params[0]) then begin
LFileName := ALSender.UnparsedParams;
//This is local Time
LgMTime := FTPMLSToGMTDateTime(Fetch(LFileName)) - OffsetFromUTC;
LgMTime := UTCTimeToLocalTime(FTPMLSToGMTDateTime(Fetch(LFileName)));
LFileName := DoProcessPath(AContext, LFileName);
if Assigned(FOnSiteUTIME) then
begin
Expand Down Expand Up @@ -6200,14 +6200,16 @@ procedure TIdFTPServer.CommandCPSV(ASender: TIdCommand);
procedure TIdFTPServer.CommandSiteZONE(ASender: TIdCommand);
var
LMin : Integer;
LFmt: string;
begin
LMin := MinutesFromGMT;
//plus must always be displayed for positive numbers
if LMin < 0 then begin
ASender.Reply.SetReply(210, IndyFormat('UTC%d', [MinutesFromGMT])); {do not localize}
LFmt := 'UTC%d'; {do not localize}
end else begin
ASender.Reply.SetReply(210, IndyFormat('UTC+%d', [MinutesFromGMT])); {do not localize}
LFmt := 'UTC+%d'; {do not localize}
end;
ASender.Reply.SetReply(210, IndyFormat(LFmt, [LMin]));
end;

procedure TIdFTPServer.CommandCheckSum(ASender: TIdCommand);
Expand Down
57 changes: 10 additions & 47 deletions Lib/Protocols/IdGlobalProtocols.pas
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ EIdExtensionAlreadyExists = class(EIdException);
function StrToDay(const ADay: string): Byte;
function StrToMonth(const AMonth: string): Byte;
function StrToWord(const Value: String): Word;
function TimeZoneBias: TDateTime;
function TimeZoneBias: TDateTime; {$IFDEF HAS_DEPRECATED}deprecated{$IFDEF HAS_DEPRECATED_MSG} 'Use IdGlobal.LocalTimeToUTCTime() or IdGlobal.UTCTimeToLocalTime()'{$ENDIF};{$ENDIF}
//these are for FSP but may also help with MySQL
function UnixDateTimeToDelphiDateTime(UnixDateTime: UInt32): TDateTime;
function DateTimeToUnix(ADateTime: TDateTime): UInt32;
Expand Down Expand Up @@ -593,28 +593,22 @@ implementation
{$ENDIF}
{$ENDIF}
IdIPAddress,
{$IFDEF HAS_GetLocalTimeOffset}
DateUtils,
{$ENDIF}
{$IFDEF UNIX}
{$IFDEF USE_VCL_POSIX}
{$IFNDEF HAS_GetLocalTimeOffset}
DateUtils,
{$ENDIF}
Posix.SysStat, Posix.SysTime, Posix.Time, Posix.Unistd,
{$ELSE}
{$IFDEF KYLIXCOMPAT}
Libc,
{$ELSE}
{$IFDEF USE_BASEUNIX}
BaseUnix, Unix,
{$IFNDEF HAS_GetLocalTimeOffset}
DateUtils,
{$ENDIF}
{$ENDIF}
{$ENDIF}
{$ENDIF}
{$ENDIF}
{$IFDEF HAS_UNIT_DateUtils}
DateUtils,
{$ENDIF}
{$IFDEF WINDOWS}
Messages,
Registry,
Expand Down Expand Up @@ -1352,7 +1346,7 @@ function FTPMLSToLocalDateTime(const ATimeStamp : String):TDateTime;
if ATimeStamp <> '' then begin
Result := FTPMLSToGMTDateTime(ATimeStamp);
// Apply local offset
Result := {$IFDEF HAS_UniversalTimeToLocal}UniversalTimeToLocal(Result){$ELSE}Result + OffsetFromUTC{$ENDIF};
Result := UTCTimeToLocalTime(Result);
end;
end;

Expand All @@ -1378,13 +1372,7 @@ function FTPGMTDateTimeToMLS(const ATimeStamp : TDateTime; const AIncludeMSecs :
function FTPLocalDateTimeToMLS(const ATimeStamp : TDateTime; const AIncludeMSecs : Boolean=True): String;
{$IFDEF USE_INLINE} inline; {$ENDIF}
begin
Result := FTPGMTDateTimeToMLS(
{$IFDEF HAS_LocalTimeToUniversal}
LocalTimeToUniversal(ATimeStamp)
{$ELSE}
ATimeStamp - OffsetFromUTC
{$ENDIF}
, AIncludeMSecs);
Result := FTPGMTDateTimeToMLS(LocalTimeToUTCTime(ATimeStamp), AIncludeMSecs);
end;


Expand Down Expand Up @@ -1981,36 +1969,12 @@ function RightStr(const AStr: String; const Len: Integer): String;
end;
end;

{$I IdDeprecatedImplBugOff.inc}
function TimeZoneBias: TDateTime;
{$IFNDEF FPC}
{$IFDEF UNIX}
var
T: Time_T;
TV: TimeVal;
UT: {$IFDEF USE_VCL_POSIX}tm{$ELSE}TUnixTime{$ENDIF};
{$ELSE}
{$IFDEF USE_INLINE} inline; {$ENDIF}
{$ENDIF}
{$ELSE}
{$I IdDeprecatedImplBugOn.inc}
{$IFDEF USE_INLINE} inline; {$ENDIF}
{$ENDIF}
begin
{$IFNDEF FPC}
{$IFDEF UNIX}
// TODO: use -OffsetFromUTC here. It has this same Unix logic in it
{from http://edn.embarcadero.com/article/27890 }
gettimeofday(TV, nil);
T := TV.tv_sec;
localtime_r({$IFNDEF USE_VCL_POSIX}@{$ENDIF}T, UT);
// __tm_gmtoff is the bias in seconds from the UTC to the current time.
// so I multiply by -1 to compensate for this.
Result := (UT.{$IFNDEF USE_VCL_POSIX}__tm_gmtoff{$ELSE}tm_gmtoff{$ENDIF} / 60 / 60 / 24);
{$ELSE}
Result := -OffsetFromUTC;
{$ENDIF}
{$ELSE}
Result := -OffsetFromUTC;
{$ENDIF}
end;

function IndyStrToBool(const AString : String) : Boolean;
Expand Down Expand Up @@ -2810,8 +2774,7 @@ function GMTToLocalDateTime(S: string): TDateTime;
if RawStrInternetToDateTime(S, Result) then begin
DateTimeOffset := GmtOffsetStrToDateTime(S);
{-Apply GMT and local offsets}
Result := Result - DateTimeOffset;
Result := {$IFDEF HAS_UniversalTimeToLocal}UniversalTimeToLocal(Result){$ELSE}Result + OffsetFromUTC{$ENDIF};
Result := UTCTimeToLocalTime(Result - DateTimeOffset);
end;
end;

Expand Down Expand Up @@ -3066,7 +3029,7 @@ function CookieStrToLocalDateTime(S: string): TDateTime;
end;

Result := EncodeDate(LYear, LMonth, LDayOfMonth) + EncodeTime(LHour, LMinute, LSecond, 0);
Result := {$IFDEF HAS_UniversalTimeToLocal}UniversalTimeToLocal(Result){$ELSE}Result + OffsetFromUTC{$ENDIF};
Result := UTCTimeToLocalTime(Result);
except
Result := 0.0;
end;
Expand Down
4 changes: 2 additions & 2 deletions Lib/Protocols/IdHTTPWebBrokerBridge.pas
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ function TIdHTTPAppResponse.GetDateVariable(Index: Integer): TDateTime;
begin
Result := ADateTime;
if Result <> -1 then
Result := Result - OffsetFromUTC;
Result := LocalTimeToUTCTime(Result);
end;
begin
//TODO: resource string these
Expand All @@ -647,7 +647,7 @@ procedure TIdHTTPAppResponse.SetDateVariable(Index: Integer; const Value: TDateT
begin
Result := ADateTime;
if Result <> -1 then
Result := Result + OffsetFromUTC;
Result := UTCTimeToLocalTime(Result);
end;
begin
//TODO: resource string these
Expand Down
8 changes: 4 additions & 4 deletions Lib/Protocols/IdNNTPServer.pas
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ class function TIdNNTPServer.NNTPTimeToTime(const ATimeStamp : String): TDateTim
if TextIsSame(LTimeStr, 'GMT') then {do not localize}
begin
// Apply local offset
Result := Result + OffsetFromUTC;
Result := UTCTimeToLocalTime(Result);
end;
end else begin
Result := 0.0;
Expand Down Expand Up @@ -652,7 +652,7 @@ procedure TIdNNTPServer.CommandBody(ASender: TIdCommand);
procedure TIdNNTPServer.CommandDate(ASender: TIdCommand);
begin
if not SecLayerRequired(ASender) then begin
ASender.Reply.SetReply(111, FormatDateTime('yyyymmddhhnnss', Now + TimeZoneBias)); {do not localize}
ASender.Reply.SetReply(111, FormatDateTime('yyyymmddhhnnss', LocalTimeToUTCTime(Now))); {do not localize}
end;
end;

Expand Down Expand Up @@ -1551,7 +1551,7 @@ procedure TIdNNTPServer.CommandNewGroups(ASender: TIdCommand);
LDate := LDate + NNTPTimeToTime(ASender.Params[1]);
if ASender.Params.Count > 2 then begin
if TextIsSame(ASender.Params[2], 'GMT') then begin {Do not translate}
LDate := LDate + OffSetFromUTC;
LDate := UTCTimeToLocalTime(LDate);
if ASender.Params.Count > 3 then begin
LDist := ASender.Params[3];
end;
Expand Down Expand Up @@ -1587,7 +1587,7 @@ procedure TIdNNTPServer.CommandNewNews(ASender: TIdCommand);
LDate := LDate + NNTPTimeToTime(ASender.Params[2]);
if ASender.Params.Count > 3 then begin
if TextIsSame(ASender.Params[3], 'GMT') then begin {Do not translate}
LDate := LDate + OffsetFromUTC;
LDate := UTCTimeToLocalTime(LDate);
if ASender.Params.Count > 4 then begin
LDist := ASender.Params[4];
end;
Expand Down
Loading

0 comments on commit a8f3ac5

Please sign in to comment.