Recent

Author Topic: [Bug] asDateTime conversion error! 4 digit milisec 2025-12-12 12:12:12.1230  (Read 1632 times)

dbannon

  • Hero Member
  • *****
  • Posts: 3667
    • tomboy-ng, a rewrite of the classic Tomboy
Re: [Bug] asDateTime conversion error! 4 digit milisec 2025-12-12 12:12:12.1230
« Reply #15 on: December 19, 2025, 01:02:31 am »
....
Bottom line: it‘s the user’s responsibility to truncate/round exceeding decimals after the 3rd decimal BEFORE sending it to any Parsing Function

Not if you use the iso8601 methods. eg -

Code: Pascal  [Select][+][-]
  1. program testdate;
  2. {$mode objfpc}
  3.  
  4. uses dateutils;
  5. var
  6. DateTime, DateTime2 : TDateTime;
  7.  
  8. begin
  9.         if TryISO8601ToDate('2025-12-19T10:45:00.123456', DateTime) then
  10.                 writeln('Convert to DateTime OK');
  11.         if TryISO8601ToDate('2025-12-19T10:45:00.123', DateTime2) then
  12.                 writeln('Convert to DateTime2 OK');
  13.         writeln('Diff is ', DateTime - DateTime2);
  14. end.
Code: Bash  [Select][+][-]
  1. dbannon@dell:~/Pascal/CLI$ ./test_date
  2. Convert to DateTime OK
  3. Convert to DateTime2 OK
  4. Diff is  5.2750692702829838E-009
Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

wp

  • Hero Member
  • *****
  • Posts: 13350
Re: [Bug] asDateTime conversion error! 4 digit milisec 2025-12-12 12:12:12.1230
« Reply #16 on: December 19, 2025, 01:25:40 am »
On Windows the first output is missing, i.e. TryISO8601ToDate('2025-12-19T10:45:00.123456', DateTime) is not OK.

cdbc

  • Hero Member
  • *****
  • Posts: 2600
    • http://www.cdbc.dk
Re: [Bug] asDateTime conversion error! 4 digit milisec 2025-12-12 12:12:12.1230
« Reply #17 on: December 19, 2025, 10:08:08 am »
Hi
On PCLinuxOS it looks like this:
Code: Bash  [Select][+][-]
  1. [bc@stinker date-stuff]$ fpc test_date.pp
  2. Free Pascal Compiler version 3.2.2 [2024/07/26] for x86_64
  3. Copyright (c) 1993-2021 by Florian Klaempfl and others
  4. Target OS: Linux for x86-64
  5. Compiling test_date.pp
  6. Linking test_date
  7. 14 lines compiled, 1.2 sec
  8. [bc@stinker date-stuff]$ ./test_date
  9. Convert to DateTime2 OK
  10. Diff is -4.6010447918090278E+004
  11. [bc@stinker date-stuff]$
  12.  
Same issue as with Werner...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

dbannon

  • Hero Member
  • *****
  • Posts: 3667
    • tomboy-ng, a rewrite of the classic Tomboy
Re: [Bug] asDateTime conversion error! 4 digit milisec 2025-12-12 12:12:12.1230
« Reply #18 on: December 19, 2025, 10:47:47 am »
Code: [Select]
[quote author=cdbc link=topic=73029.msg572609#msg572609 date=1766135288]
On PCLinuxOS it looks like this ...
[bc@stinker date-stuff]$ ./test_date
Convert to DateTime2 OK
Diff is -4.6010447918090278E+004

Oh, my code should report an error as well as success !  I and a little surprised WP had a problem on Windows (I use same basic idea in my app and its cross platform). But really surprised with PCLinux.

Will do some experimenting ....

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Zvoni

  • Hero Member
  • *****
  • Posts: 3242
Re: [Bug] asDateTime conversion error! 4 digit milisec 2025-12-12 12:12:12.1230
« Reply #19 on: December 19, 2025, 11:05:10 am »
Code: Pascal  [Select][+][-]
  1. program testdate;
  2. {$mode objfpc}
  3.  
  4. uses dateutils;
  5. var
  6. DateTime, DateTime2 : TDateTime;
  7.  
  8. begin
  9.         if TryISO8601ToDate('2025-12-19T10:45:00.123456', DateTime) then
  10.                 writeln('Yeet DateTime') Else Writeln('No Yeet Datetime');
  11.         if TryISO8601ToDate('2025-12-19T10:45:00.123', DateTime2) then
  12.                 writeln('Yeet DateTime2') Else Writeln('No Yeet Datetime2');
  13.         writeln('Diff is ', DateTime - DateTime2);
  14.         Readln;
  15. end.
Returns
Code: [Select]
Yeet DateTime
Yeet DateTime2
Diff is  5.2750692702829838E-009

Win10-64-Bits - FPC3.2.3/Laz4.3 (old fixes)
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

paweld

  • Hero Member
  • *****
  • Posts: 1568
Re: [Bug] asDateTime conversion error! 4 digit milisec 2025-12-12 12:12:12.1230
« Reply #20 on: December 19, 2025, 11:22:57 am »
As @Zvoni wrote, it is OK in FPC 3.2-fixes, but in FPC 3.2.2 TryISO8601ToDate(‘2025-12-19T10:45:00.123456’, DateTime) is not. Tested on Windows 10.
Best regards / Pozdrawiam
paweld

dbannon

  • Hero Member
  • *****
  • Posts: 3667
    • tomboy-ng, a rewrite of the classic Tomboy
Re: [Bug] asDateTime conversion error! 4 digit milisec 2025-12-12 12:12:12.1230
« Reply #21 on: December 19, 2025, 11:34:11 am »

Ah, yes paweld, just writing up the same explanation.

WP, Benny, are you using FPC322 ?

Looks like this is one of the many things that have been fixed in FPC323. I took my binary (compiled with fpc324rc1) to a PCLinux VM and it worked as expected. So I took the source and compiled it on PCLinux and it fails.

So, looking in the source of my App (which must compile with FPC322 on Debian) I realize I work around that problem. I vaguely recall reporting the issue and 'someone' fixed it, possibly PascalDragon. But of course, its not in the "Released Version" of FPC yet. Sigh ....

Code: Pascal  [Select][+][-]
  1. function MyTryISO8601ToDate(DateSt : string; out OutDT : TDateTime; ReturnUTC : boolean = true) : boolean;
  2. var
  3.     I : integer;
  4.     St : string = '';
  5. begin
  6.     OutDT := 0.0;
  7.     if DateSt = '' then exit(False);
  8.     Result := True;
  9.     I := pos('.', DateSt);                          // if we have decimal point, we have stuff to do.
  10.     if I > 0 then begin                             // TryISO8601ToDate cannot handle string with decimals of a second
  11.         delete(DateSt, I, 1);                       // Remove decimal point
  12.         while I  < length(DateSt) do begin
  13.             if DateSt[I] in ['0'..'9'] then begin
  14.                 St := St + DateSt[I];               // save digits to use later
  15.                 delete(DateSt, I, 1);
  16.             end else break;
  17.         end;
  18.         // The first six digits in St represent microseconds. we will stop there.
  19.         while length(St) > 6 do delete(St, length(St), 1);
  20.         while length(St) < 6 do St := St + '0';
  21.     end;
  22.     if TryISO8601ToDate(DateSt, OutDT, ReturnUTC) then begin               // WARNING - apparently this is a FPC320 only feature
  23.         if I > 0 then
  24.             OutDT := OutDT + (St.ToDouble() * ValueMicroSecond);           // ValueMicroSecond is Regional const,  eg
  25.         end else result := False;                                              // ValueMicroSecond := 1.0 / double(24*60*60*1000*1000);
  26. end;
                                                                                                                                   

Davo

Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

cdbc

  • Hero Member
  • *****
  • Posts: 2600
    • http://www.cdbc.dk
Re: [Bug] asDateTime conversion error! 4 digit milisec 2025-12-12 12:12:12.1230
« Reply #22 on: December 19, 2025, 01:11:06 pm »
Hi Davo
Right, sorry I was a lazy bastard...
This time compiled with 2 weeks old 'main' ~ fpc 3.3.1:
Code: Bash  [Select][+][-]
  1. [bc@stinker date-stuff]$ ./test_date
  2. Convert to DateTime OK
  3. Convert to DateTime2 OK
  4. Diff is  5.2750692702829838E-009
  5. [bc@stinker date-stuff]$
  6.  
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

 

TinyPortal © 2005-2018