Recent

Author Topic: [SOLVED] TimeStampToMSecs function returns an invalid value on win32  (Read 899 times)

korba812

  • Sr. Member
  • ****
  • Posts: 475
I just updated the win32 compiler (trunk) and noticed that timestamp database fields are incorrectly displayed. After analysis, the problem is TimeStampToMSecs function, which returns an invalid value. However, if I modify this function and use a constant cast to Int64 type then function returns correct value.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses
  4.   SysUtils;
  5.  
  6. // from Sysutils.TimeStampToMSecs
  7. function TimeStampToMSecs1(const TimeStamp: TTimeStamp): Int64;
  8. begin
  9.   result := TimeStamp.Time + timestamp.date * msecsperday;
  10. end;
  11.  
  12. function TimeStampToMSecs2(const TimeStamp: TTimeStamp): Int64;
  13. begin
  14.   result := TimeStamp.Time + timestamp.date * Int64(msecsperday);
  15. end;
  16.  
  17. var
  18.   TS: TTimeStamp = (Time: 40811000; Date: 738998);
  19.  
  20. begin
  21.   WriteLn(TimeStampToMSecs1(TS));
  22.   WriteLn(TimeStampToMSecs2(TS));
  23.   ReadLn;
  24. end.
  25.  

Where does the problem lie? Compiler bug or lack of casting in the TimeStampToMSecs function?
« Last Edit: April 22, 2024, 06:44:56 pm by korba812 »

alpine

  • Hero Member
  • *****
  • Posts: 1391
Re: TimeStampToMSecs function returns an invalid value on win32
« Reply #1 on: April 22, 2024, 05:39:30 pm »
Where does the problem lie? Compiler bug or lack of casting in the TimeStampToMSecs function?

Arithmetic overflow into the first one. Obviously it is the casting.

BTW, compiler gives a hint:
project1.lpr(9,10) Hint: Converting the operands to "Int64" before doing the add could prevent overflow errors.
« Last Edit: April 22, 2024, 05:44:54 pm by alpine »
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

korba812

  • Sr. Member
  • ****
  • Posts: 475
Re: TimeStampToMSecs function returns an invalid value on win32
« Reply #2 on: April 22, 2024, 06:23:33 pm »
Ah, now I see. I just checked the stable release sources and there TimeStampToMSecs function returns a value of comp type. In trunk it has been changed to Int64 and indeed requires a cast. I will report this in the bugtraker.

..edit..

The problem has already been solved in trunk, and I accidentally use old sources... Sorry for the noise.
« Last Edit: April 22, 2024, 06:46:46 pm by korba812 »

 

TinyPortal © 2005-2018