Recent

Author Topic: Writing to datetime field in dbf  (Read 2973 times)

wp

  • Hero Member
  • *****
  • Posts: 7930
Writing to datetime field in dbf
« on: July 23, 2016, 09:45:17 pm »
Here's another issue that I stumbled across. TDbf fails to post a datetime field. Please run this code, it will crash with a SIGFPE in the line where the current date is assigned to the field:

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   SysUtils, Classes, dbf, dbf_common, db, LazFileUtils;
  7.  
  8. var
  9.   Dbf1: TDbf;
  10.  
  11. begin
  12.   ForceDirectories('data');
  13.   DeleteFile(AppendPathDelim('data') + 'table1.dbf');
  14.  
  15.   Dbf1 := TDbf.Create(nil);
  16.   try
  17.     Dbf1.FilePath := AppendPathDelim('data');
  18.     Dbf1.TableLevel :=7;
  19.     Dbf1.Exclusive := True;
  20.     Dbf1.TableName := 'table1.dbf';
  21. //    Dbf1.DateTimeHandling := dtDateTime;         /// no effect
  22. //    Dbf1.DateTimeHandling := dtBDETimeStamp;
  23.     With Dbf1.FieldDefs do begin
  24.       Add('Date', ftDateTime, 0, false);   // no issue with ftDate
  25.     end;
  26.     Dbf1.CreateTable;
  27.     Dbf1.Open;
  28.     Dbf1.Append;
  29.     Dbf1.FieldByName('Date').AsDateTime := now;     // <-- SIGFPE here
  30.     Dbf1.Post;
  31.   finally
  32.     Dbf1.Free;
  33.   end;
  34.  
  35.   ReadLn;
  36. end.  

Changing the DateTimeHandling mentioned in the manual has no effect. The table, however, is posted without issues if the field is created as a ftDate field - but of course this strips the time part.

Could it be that TDbf does not support date/time fields? But according to http://wiki.freepascal.org/Lazarus_Tdbf_Tutorial#Adding_fields it does.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

LuizAmérico

  • Sr. Member
  • ****
  • Posts: 458
Re: Writing to datetime field in dbf
« Reply #1 on: July 23, 2016, 11:23:50 pm »
A bug was fixed recently. http://bugs.freepascal.org/view.php?id=28734

Can you try trunk?

wp

  • Hero Member
  • *****
  • Posts: 7930
Re: Writing to datetime field in dbf
« Reply #2 on: July 24, 2016, 12:39:01 am »
Yes, the bug is fixed in fpc trunk - thanks for the hint.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

 

TinyPortal © 2005-2018