DateUtils LocalTimeToUniversal and UniversalTimeToLocal
Original Reporter info from Mantis: dinmil
-
Reporter name: Dinko Miljak
Original Reporter info from Mantis: dinmil
- Reporter name: Dinko Miljak
Description:
I have problem with these functions from dateutils.
It seems to me that add and subtract operators are not correct in these functions.
Check out this little code for test. On windows environment and my timezone I can not get correct result.
Maybe somebody can confirm this.
I CodeTyphon 5.4.0 with FPC 3.1.1. It seem to me that this is problem of fpc source.
procedure TForm1.TestUTC;
var MyLocalDate: TDateTime;
MyUTCDate: TDateTime;
begin
MyLocalDate := Now;
MyUTCDate := dateutils.LocalTimeToUniversal(MyLocalDate);
ShowMessage('LocalDate: ' + FormatDateTime('yyyy-mm-dd hh:nn:ss.zzzzz', MyLocalDate) + #13#10 +
'UTCDate: ' + FormatDateTime('yyyy-mm-dd hh:nn:ss.zzzzz', MyUTCDate));
end;
I this that correct code should be like that:
function UniversalTimeToLocal(UT: TDateTime; TZOffset : Integer): TDateTime;
begin
if (TZOffset > 0) then
Result := UT - EncodeTime(TZOffset div 60, TZOffset mod 60, 0, 0)
else if (TZOffset < 0) then
Result := UT + EncodeTime(Abs(TZOffset) div 60, Abs(TZOffset) mod 60, 0, 0)
else
Result := UT;
end;
Function LocalTimeToUniversal(LT: TDateTime;TZOffset: Integer): TDateTime;
begin
if (TZOffset > 0) then
Result := LT + EncodeTime(TZOffset div 60, TZOffset mod 60, 0, 0)
else if (TZOffset < 0) then
Result := LT - EncodeTime(Abs(TZOffset) div 60, Abs(TZOffset) mod 60, 0, 0)
else
Result := LT;
end;
Regards, Dinko
Steps to reproduce:
Code which will reproduce error
procedure TForm1.TestUTC;
var MyLocalDate: TDateTime;
MyUTCDate: TDateTime;
begin
MyLocalDate := Now;
MyUTCDate := dateutils.LocalTimeToUniversal(MyLocalDate);
ShowMessage('LocalDate: ' + FormatDateTime('yyyy-mm-dd hh:nn:ss.zzzzz', MyLocalDate) + #13#10 +
'UTCDate: ' + FormatDateTime('yyyy-mm-dd hh:nn:ss.zzzzz', MyUTCDate));
end;
Mantis conversion info:
- Mantis ID: 28534
- OS: Windows, All Others Probably
- OS Build: Windows 7
- Platform: All
- Version: 3.1.1
- Fixed in version: 3.1.1
- Fixed in revision: 31356 (#82a825af)
- Monitored by: » herux (herux)
- Target version: 3.0.0