View Issue Details

IDProjectCategoryView StatusLast Update
0031954FPCDocumentationpublic2017-06-04 14:44
ReporterAbou Al MontacirAssigned ToMichael Van Canneyt 
PrioritynormalSeveritymajorReproducibilityalways
Status closedResolutionfixed 
Platformx86_64OSDebian GNU/LinuxOS VersionStretch
Product Version3.0.0Product Build3.0.0+dfsg-11 
Target Version3.0.0Fixed in Version3.0.2 
Summary0031954: Incorrect conversion from local time to UTC
DescriptionI'm trying to play with local time / UTC conversion and come to a strange issue
I'm using Linux and seems that the conversion is done in the opposite side
I mean that LocalTimeToUniversal(Now); gives me LT + 2 instead of LT - 2 (I'm in Paris)
Steps To Reproduceas easy as:
$ifpc /tmp/utc.pas
Local TS = 2017-06-02 19:33:614
UTC TS = 2017-06-02 21:33:614
GetLocalTimeOffset = -120
Additional InformationLooking at code I see:
Function LocalTimeToUniversal(LT: TDateTime): TDateTime;

begin
  Result:=LocalTimeToUniversal(LT,GetLocalTimeOffset);
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;
However this is probably wrong as according to help of GetLocalTimeOffset we have: UTC = LocalTime + GetLocalTimeOffset
So probably we should add - (minus sign) when calling GetLocalTimeOffset
Am'I missing something?
TagsNo tags attached.
Fixed in Revision
FPCOldBugId0
FPCTarget
Attached Files
  • utc.pas (308 bytes)
    program utc;
    uses SysUtils, DateUtils;
    
    const
    	fmt = 'yyyy-mm-dd HH:M:MSS';
    
      var
    	TS: TDateTime;
    begin
    	TS := Now;
    	WriteLn('Local TS = ', FormatDateTime(fmt, TS));
    	TS := LocalTimeToUniversal(TS);
    	WriteLn('UTC TS = ', FormatDateTime(fmt, TS));
    	WriteLn('GetLocalTimeOffset = ', GetLocalTimeOffset);
    end.
    
    utc.pas (308 bytes)

Activities

Abou Al Montacir

2017-06-02 19:34

manager  

utc.pas (308 bytes)
program utc;
uses SysUtils, DateUtils;

const
	fmt = 'yyyy-mm-dd HH:M:MSS';

  var
	TS: TDateTime;
begin
	TS := Now;
	WriteLn('Local TS = ', FormatDateTime(fmt, TS));
	TS := LocalTimeToUniversal(TS);
	WriteLn('UTC TS = ', FormatDateTime(fmt, TS));
	WriteLn('GetLocalTimeOffset = ', GetLocalTimeOffset);
end.
utc.pas (308 bytes)

Michael Van Canneyt

2017-06-02 19:45

administrator   ~0100803

The output of the program is correct.
The documentation needs to be adapted.

There are some related bugs in the bugtracker.

jamie philbrook

2017-06-02 20:46

reporter   ~0100804

There are two functions..
LocalTimeToUniversal(Now); // ADDs to time.
LocalTimeToUniversal(Now, GetLocalTimeOffset); // Subtracts time.

The GetlocalTimeOffset returns 240 for me, which is correct for my location
with DST.

 I would think both Functions would require the same sign number..?
 Which one is correct?

jamie philbrook

2017-06-02 21:35

reporter   ~0100805

After looking at some old code, it appears the first shorter function is
correct.

 My time bias is (300)-60 for the hour change = +240
To get local its UTC - yourBias = LocalTime and to get UTC from local its Local + Bias = UTC time.

The second function that accepts offset fails here to, you shouldn't need to
specify a -Bias.

 The second function would work if you were going the other way. UTC to Local.

Abou Al Montacir

2017-06-03 10:02

manager   ~0100816

I've tried the following:

$date
Sat Jun 3 09:57:33 CEST 2017

$date -u
Sat Jun 3 07:57:36 UTC 2017

$ifpc ~/Downloads/utc.pas
Local TS = 2017-06-03 09:58:616
UTC TS = 2017-06-03 11:58:616
GetLocalTimeOffset = -120

I'm expecting UTC time to be local time -2 while it is here local time +2 just like you can see as output of date -u.

So can you explain more how the program output is correct?

Michael Van Canneyt

2017-06-03 10:18

administrator   ~0100817

Hm. Here it displays

Local TS = 2017-06-03 10:15:649
UTC TS = 2017-06-03 08:15:649
GetLocalTimeOffset = -120

As expected?

Michael Van Canneyt

2017-06-03 10:19

administrator   ~0100818

I only see now you used 3.0.
Try the trunk version or 3.0.2.

There was a bug that is fixed as of 3.0.2

Abou Al Montacir

2017-06-04 13:44

manager   ~0100851

I need to back port this to 3.0.0 for Debian Stretch (going to be released soon).

I've checked commits and r31356 seems to be the one you are talking about, but I need to know if that is the only one or does it require other changes?

Can you please advise on what are the commits I need to take in order to ensure a correct support for UTC time on 3.0.0?

Michael Van Canneyt

2017-06-04 14:12

administrator   ~0100854

r31356 seems to be the only one you need to merge, yes.

Abou Al Montacir

2017-06-04 14:44

manager   ~0100855

Patch from r31356 fixes indeed the issue.

The patch will be ported to 3.0.0 on Debian.

Issue History

Date Modified Username Field Change
2017-06-02 19:34 Abou Al Montacir New Issue
2017-06-02 19:34 Abou Al Montacir Status new => assigned
2017-06-02 19:34 Abou Al Montacir Assigned To => Michael Van Canneyt
2017-06-02 19:34 Abou Al Montacir File Added: utc.pas
2017-06-02 19:39 Abou Al Montacir Severity minor => major
2017-06-02 19:39 Abou Al Montacir OS Debian => Debian GNU/Linux
2017-06-02 19:40 Abou Al Montacir Additional Information Updated View Revisions
2017-06-02 19:44 Michael Van Canneyt Category RTL => Documentation
2017-06-02 19:45 Michael Van Canneyt Note Added: 0100803
2017-06-02 20:46 jamie philbrook Note Added: 0100804
2017-06-02 21:35 jamie philbrook Note Added: 0100805
2017-06-03 10:02 Abou Al Montacir Note Added: 0100816
2017-06-03 10:18 Michael Van Canneyt Note Added: 0100817
2017-06-03 10:19 Michael Van Canneyt Note Added: 0100818
2017-06-04 13:44 Abou Al Montacir Note Added: 0100851
2017-06-04 14:12 Michael Van Canneyt Note Added: 0100854
2017-06-04 14:44 Abou Al Montacir Note Added: 0100855
2017-06-04 14:44 Abou Al Montacir Status assigned => closed
2017-06-04 14:44 Abou Al Montacir Resolution open => fixed
2017-06-04 14:44 Abou Al Montacir Fixed in Version => 3.0.2