Recent

Author Topic: TDBDateTimePicker and ISO8601...?  (Read 3904 times)

Sieben

  • Sr. Member
  • ****
  • Posts: 310
TDBDateTimePicker and ISO8601...?
« on: November 17, 2021, 09:58:36 pm »
@Zoran - I thought it would be nice to have TDBDateTimePicker read and write ISO8601 date and time formats when connected to a ftString field, especially when working with a database like SQLite3 which doesn't offer a dedicated date/time data type but functions to operate on ISO string representations (cf here). This would integrate quite seamlessly with TDBDateTimePicker itself like this (plus conversion routines, of course):

Code: Pascal  [Select][+][-]
  1. procedure TDBDateTimePicker.DataChange(Sender: TObject);
  2. var ADateTime: TDateTime; //rkmod
  3. begin
  4.   if (FChangingCount = 0) then begin
  5.     Inc(FDataChangeCount);
  6.     try
  7.       if Assigned(FDataLink.Field) and not FDataLink.Field.IsNull then begin
  8.  
  9.         if FISOMode and (FDataLink.Field.DataType = ftString) then //rkmod
  10.           ADateTime := ISOToDateTime(FDataLink.Field.AsString)
  11.         else
  12.           ADateTime := FDataLink.Field.AsDateTime;
  13.  
  14.         // Using the SetTheDateJumpMinMax procedure, instead of property
  15.         SetDateTimeJumpMinMax(ADateTime); // assignment allows
  16.             // this control to display dates from database whose value falls
  17.             // outside of MinDate and MaxDate interval.
  18.             // Note that user still cannot enter such values in the control.
  19.       end else
  20.         DateTime := NullDate;
  21.  
  22.     finally
  23.       Dec(FDataChangeCount);
  24.     end;
  25.   end;
  26. end;
  27.  
  28. //...
  29.  
  30. procedure TDBDateTimePicker.UpdateData(Sender: TObject);
  31. begin
  32.   if Assigned(FDataLink.Field) then begin
  33.     if DateIsNull then
  34.       FDataLink.Field.AsVariant := Null
  35.     else begin
  36.       if FISOMode and (FDataLink.Field.DataType = ftString) then //rkmod
  37.         FDataLink.Field.AsString := DateTimeToISO(DateTime)
  38.       else
  39.         FDataLink.Field.AsDateTime := DateTime;
  40.     end;
  41.   end;
  42. end;
  43.  

Or it might as well be done with a descendant, of course. However, to share it this would require to move some fields and procs of TDBDateTimePicker from private section to protected, namely FDataLink, FChangingCount, FDataChangeCount and of course procs DataChange and UpdateData which would also have to be declared as virtual. I'm including my working copy of said descendant here, maybe you might want to have a look...?

Edit - sorry, I thought I had included the attachment...
« Last Edit: November 17, 2021, 10:48:30 pm by Sieben »
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: TDBDateTimePicker and ISO8601...?
« Reply #1 on: November 20, 2021, 07:21:19 pm »
I have just seen this topic. I will take a look when I have time.
Thank you, Sieben.

 

TinyPortal © 2005-2018