View Issue Details

IDProjectCategoryView StatusLast Update
0012894FPCRTLpublic2016-11-27 19:03
ReporterSeth Grover Assigned ToJonas Maebe  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
Product Version2.2.2 
Fixed in Version2.4.0 
Summary0012894: DateTimeToUnix rounding error
DescriptionI'm using FPC 2.2.2 on Linux (2.6.27-9-generic 0000001 SMP Thu Nov 20 21:57:00 UTC 2008 i686 GNU/Linux).

==================================================
program Project1;

uses
 Classes, SysUtils, DateUtils;

var
 utime : longword;
 sec : word;
 currentDt, convertedDt : TDateTime;

begin
 for sec := 0 to 59 do begin
   currentDt := EncodeDateTime(1989, 9, 16, 12, 0, sec, 0);
   utime := DateTimeToUnix(currentDt);
   convertedDt := UnixToDateTime(utime);
   writeln(FormatDateTime('mm/dd/yyyy HH:nn:ss', currentDt) + ' = ' +
     IntToStr(utime) + ' = ' +
     FormatDateTime('mm/dd/yyyy HH:nn:ss',
     convertedDt));
 end;
end.
==================================================

This seems to be rounding error - if you put 1 ms (milisecond) in the
seconds test, the result comes out right.

For consecutive seconds, and ms = 0:

09-16-1989 12:00:00 = 621950400 = 09-16-1989 12:00:00
09-16-1989 12:00:01 = 621950400 = 09-16-1989 12:00:00
09-16-1989 12:00:02 = 621950402 = 09-16-1989 12:00:02
09-16-1989 12:00:03 = 621950403 = 09-16-1989 12:00:03
09-16-1989 12:00:04 = 621950403 = 09-16-1989 12:00:03
09-16-1989 12:00:05 = 621950404 = 09-16-1989 12:00:04
09-16-1989 12:00:06 = 621950406 = 09-16-1989 12:00:06
09-16-1989 12:00:07 = 621950407 = 09-16-1989 12:00:07
09-16-1989 12:00:08 = 621950407 = 09-16-1989 12:00:07
09-16-1989 12:00:09 = 621950408 = 09-16-1989 12:00:08
09-16-1989 12:00:10 = 621950410 = 09-16-1989 12:00:10
09-16-1989 12:00:11 = 621950411 = 09-16-1989 12:00:11
09-16-1989 12:00:12 = 621950411 = 09-16-1989 12:00:11
09-16-1989 12:00:13 = 621950412 = 09-16-1989 12:00:12

with ms = 1:

09-16-1989 12:00:00 = 621950400 = 09-16-1989 12:00:00
09-16-1989 12:00:01 = 621950401 = 09-16-1989 12:00:01
09-16-1989 12:00:02 = 621950402 = 09-16-1989 12:00:02
09-16-1989 12:00:03 = 621950403 = 09-16-1989 12:00:03
09-16-1989 12:00:04 = 621950404 = 09-16-1989 12:00:04
09-16-1989 12:00:05 = 621950405 = 09-16-1989 12:00:05
09-16-1989 12:00:06 = 621950406 = 09-16-1989 12:00:06
09-16-1989 12:00:07 = 621950407 = 09-16-1989 12:00:07
09-16-1989 12:00:08 = 621950408 = 09-16-1989 12:00:08
09-16-1989 12:00:09 = 621950409 = 09-16-1989 12:00:09
09-16-1989 12:00:10 = 621950410 = 09-16-1989 12:00:10
09-16-1989 12:00:11 = 621950411 = 09-16-1989 12:00:11
09-16-1989 12:00:12 = 621950412 = 09-16-1989 12:00:12
TagsNo tags attached.
Fixed in Revision12958
FPCOldBugId
FPCTarget
Attached Files

Relationships

related to 0012936 closedJonas Maebe dateutils: function DateTimeToUnix miscalculates 
related to 0030697 resolvedMichael Van Canneyt Unix time conversion routines one second off 

Activities

Danny Milosavljevic

2009-01-04 02:16

reporter   ~0024147

It is a rounding error. Check out "m63.pp" which was created by copying the relevant portion from the FPC 2.2.2 RTL into it and adding sample code...
The output is from the "SecondsBetween" call which determines the interval between two times (the UNIX epoch as reference and the current date):

--------
SecondsBetween => 6.2195040000000000E+0008
09-16-1989 12:00:00 = 621950400 = 09-16-1989 12:00:00
SecondsBetween => 6.2195040099999992E+0008
09-16-1989 12:00:01 = 621950400 = 09-16-1989 12:00:00
An unhandled exception occurred at $BF83E548 :
EAssertionFailed : Assertion failed (m63.pp, line 69)
  $BF83E548
--------

Note that the second result is just a small fractional part below the expected value.

2009-01-04 02:16

 

m63.pp (2,423 bytes)

Issue History

Date Modified Username Field Change
2008-12-31 14:40 Seth Grover New Issue
2009-01-04 02:16 Danny Milosavljevic Note Added: 0024147
2009-01-04 02:16 Danny Milosavljevic File Added: m63.pp
2009-01-21 21:39 Jonas Maebe Relationship added related to 0012936
2009-03-22 19:27 Jonas Maebe Fixed in Revision => 12958
2009-03-22 19:27 Jonas Maebe Status new => resolved
2009-03-22 19:27 Jonas Maebe Fixed in Version => 2.3.1
2009-03-22 19:27 Jonas Maebe Resolution open => fixed
2009-03-22 19:27 Jonas Maebe Assigned To => Jonas Maebe
2009-04-02 03:32 Seth Grover Status resolved => closed
2016-11-27 19:03 Jonas Maebe Relationship added related to 0030697