Recent

Author Topic: TFormatSettings difficulties  (Read 1042 times)

MoellerCLaus

  • Full Member
  • ***
  • Posts: 114
    • Vig Foreningsprogram
TFormatSettings difficulties
« on: January 15, 2021, 02:44:19 pm »
For this work I am on now I need to enter sports times in milliseconds.
I decided to use DateTimepicker which works great and makes it possible to show minutes:sec:ms and edit and so on.
I need to come from string to datetime and back. I use SQLlite3 to store the records as floats.

Please have a look at the code
Can I avoid to use FormatSettings

Code: Pascal  [Select][+][-]
  1. unit TestUnit;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   DateTimePicker;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     DateTimePicker1: TDateTimePicker;
  18.     procedure Button1Click(Sender: TObject);
  19.     procedure FormCreate(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.Button1Click(Sender: TObject);
  36. Var HelpFormat : TFormatSettings;
  37.   HelpStr : String;
  38.   HelpTime : TDateTime;
  39. begin
  40.   HelpFormat := DefaultFormatSettings;
  41.   HelpFormat.TimeSeparator:=':';
  42.   HelpFormat.ShortTimeFormat:='nn:ss:zzz';
  43.   ShowMessage(TimeToStr(DateTimePicker1.Time,HelpFormat)); // Display hours too
  44.   HelpSTr := FormatDateTime('nn:ss:zzz',DateTimePicker1.Time); // Works
  45.   Helptime := StrToDateTime(HelpSTr,HelpFormat);  // Gives Econvert error that millisec is not time format
  46. end;
  47.  
  48. procedure TForm1.FormCreate(Sender: TObject);
  49. begin
  50.   DateTimePicker1.Kind := dtkTime;
  51.   DateTimePicker1.HideDateTimeParts := DateTimePicker1.HideDateTimeParts + [dtpHour]
  52.     - [dtpMiliSec];
  53.   DateTimePicker1.TimeDisplay:=tdHMSMs;
  54. end;
  55.  
  56. end.
  57.  

sstvmaster

  • Sr. Member
  • ****
  • Posts: 299
Re: TFormatSettings difficulties
« Reply #1 on: January 15, 2021, 04:29:25 pm »
Hi MoellerCLaus,

Try this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. Var HelpFormat : TFormatSettings;
  3.   HelpStr : String;
  4.   HelpTime : TDateTime;
  5. begin
  6.   HelpFormat := DefaultFormatSettings;
  7.   HelpFormat.TimeSeparator:=':';
  8.   HelpFormat.DecimalSeparator := '.'; // <- also needed
  9.   HelpFormat.LongTimeFormat:='nn:ss.zzz'; // you must add the dot between ss and zzz
  10.   ShowMessage(TimeToStr(DateTimePicker1.Time, HelpFormat)); // Display hours too
  11.   HelpSTr := FormatDateTime('hh:nn:ss.zzz', DateTimePicker1.Time); // <- here you must add hour and the dot
  12.   // StrToDateTime -> StrToTime
  13.   Helptime := StrToTime(HelpSTr,HelpFormat);  // <- should now work
  14. end;
  15.  

EDIT

ShortTimeFormat -> LongTimeFormat
« Last Edit: January 15, 2021, 04:57:00 pm by sstvmaster »
greetings Maik

Windows 10,
- Lazarus 2.2.6 (stable) + fpc 3.2.2 (stable)
- Lazarus 2.2.7 (fixes) + fpc 3.3.1 (main/trunk)

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: TFormatSettings difficulties
« Reply #2 on: January 15, 2021, 04:30:25 pm »
Use ScanDateTime instead of StrToDate. Together with FormatDateTime you don't need DefaultFormatSettings.

The following code works for me although my DefaultFormatSettings have quite different parameter for the time format:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses
  4.   SysUtils, DateUtils;
  5.  
  6. const
  7.   TIME_FORMAT = 'h-n-s|zzz';
  8.  
  9. var
  10.   t, t1: TDateTime;
  11.   st: String;
  12. begin
  13.   t := EncodeTime(1,2,3,456); //Now();
  14.   WriteLn('t: ', frac(t));
  15.   st := FormatDateTime(TIME_FORMAT, t);
  16.   WriteLn(st);
  17.  
  18.   t1 := ScanDateTime(TIME_FORMAT, st);
  19.   WriteLn('t1: ', frac(t1));
  20.   st := FormatDateTime(TIME_FORMAT, t1);
  21.   WriteLn(st);
  22.  
  23.   ReadLn;
  24. end.

MoellerCLaus

  • Full Member
  • ***
  • Posts: 114
    • Vig Foreningsprogram
Re: TFormatSettings difficulties
« Reply #3 on: January 15, 2021, 06:50:54 pm »
 :) Thanks to your both.
Both ideas seem to work for me.
Was indeed on my way to make a new version of Cindy TimeEdit to include ms. Well.
To solve obstacles by making own components - is often wrong way out.
Thanks for your sharing - highly appreciated.

Claus

 

TinyPortal © 2005-2018