[SOLVED] Timer lazarus / 31-LCL ---------------------------------------------------------------------- Pe3s · 2023-03-21 19:36 Hello, I wanted to try to write a timer and it's easy with seconds. What do I need to do to separate hours, minutes, seconds Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  Form1: TForm1;  TimeOut: TDateTime; implementation {$R *.lfm} function CTimer(ASecs: Integer): String;begin  Result := Format('%2d:%2.2d:%2.2d', [ASecs div 3600, ASecs mod 3600 div 60, ASecs mod 3600 mod 60]);end; { TForm1 } procedure TForm1.Timer1Timer(Sender: TObject);begin  Label1.Caption := CTimer(SecondsBetween(Now, TimeOut));  if Now > Timeout then  Timer1.Enabled := False;end; procedure TForm1.Button1Click(Sender: TObject);var  Seconds: integer;begin  if TryStrToInt(Edit1.Text, Seconds) then  begin    TimeOut := IncSecond(Now, Seconds);    Timer1.Enabled := True;    Label1.Caption := CTimer(SecondsBetween(Now, TimeOut));  end  else    ShowMessage('Error in number of seconds');end; ---------------------------------------------------------------------- KodeZwerg · 2023-03-21 20:11 To generate from seconds a HH:MM:SS string, you can do: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  Secs: QWord;begin  if TryStrToQWord(Edit1.Text, Secs) then    begin      Label1.Caption := FormatDateTime('hh:nn:ss', Secs / SecsPerDay);    end;end; ---------------------------------------------------------------------- Pe3s · 2023-03-21 20:42 @KodeZwerg, I mean how to separate setting the timer timer separately hours, minutes, seconds? ---------------------------------------------------------------------- KodeZwerg · 2023-03-21 20:57 EncodeTime(Hour, Minute, Second, MilliSecond) ? I still have no idea what you actual want. ---------------------------------------------------------------------- KodeZwerg · 2023-03-21 20:59 Or the opposite... DecodeTime(TDateTime, Hour, Minute, Second, MilliSecond) ? ---------------------------------------------------------------------- cdbc · 2023-03-21 21:41 Hi Given the granularity of ttimer is 1 millisecond... Maybe these two methods can be of use to you: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i { first from milliseconds to datetime }procedure TIsoTime.set_AsInteger(const aValue: ptrint); //bmvar  H,M,S,MSec: word;  L: cardinal; // leftover can be a pretty high number :-)begin  // 14.08.2022 new implementation, stored as milliseconds per day  // formula: fIntTime = ((H*3600000) + (M*60000) + (S*1000) + MSec  //          H = fIntTime div 3600000; L = fIntTime mod 3600000  //          M = L div 60000; L = L mod 60000  //          S = L div 1000  //          MSec = L mod 1000  if aValue <> fIntTime then begin    fIntTime:= aValue;    H:= fIntTime div 3600000; L:= fIntTime mod 3600000;    M:= L div 60000; L:= L mod 60000;    S:= L div 1000;    MSec:= L mod 1000;    fTime:= EncodeTime(H,M,S,MSec);  end;end; And from datetime to milliseconds: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TIsoTime.set_AsTime(const aValue: TDateTime);var  H,M,S,MSec: word;begin  if aValue <> fTime then begin    H:= 0; M:= 0; S:= 0; MSec:= 0;    DecodeTime(aValue,H,M,S,MSec);    fIntTime:= ((H*3600000)+(M*60000)+(S*1000)+MSec); { changed to include msecs //bm }    fTime:= EncodeTime(H,M,S,MSec);  end;end; So, get the hours, minutes, seconds & milliseconds from your user and calculate the "fIntTime" above and use that in your timer value... The procedures should be turned into functions for ease of use in your case. Hth - Regards Benny ---------------------------------------------------------------------- Pe3s · 2023-03-22 15:26 @KodeZwerg, I'm talking about setting the countdown time, I want to set hours, minutes and seconds separately. 1.spinedit - hour 2.spinedit - minutes 3.spinedit - seconds ---------------------------------------------------------------------- KodeZwerg · 2023-03-22 15:51 There is no possibility to set up the timer like you want it (with seperated values) but you can of course count them up and tell timer that value. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  H, M, S: Integer;  Secs: UInt64;begin  if (TryStrToInt(Edit1.Text, H) and TryStrToInt(Edit2.Text, M) and TryStrToInt(Edit3.Text, S)) then    begin      Secs := S;      Secs := (M * SecsPerMin) + Secs;      Secs := (H * SecsPerHour) + Secs;      Label1.Caption := IntToStr(Secs);    Timer1.Interval := Secs * 1000;    end;end; ---------------------------------------------------------------------- Pe3s · 2023-03-22 17:30 Thank you for your help ---------------------------------------------------------------------- Pe3s · 2023-03-23 09:54 I noticed that TTimer when setting the interval: 1000, distorts the time, is there any advice for this? ---------------------------------------------------------------------- KodeZwerg · 2023-03-23 10:08 Quote from: Pe3s on March 23, 2023, 10:54:59 amI noticed that TTimer when setting the interval: 1000, distorts the time, is there any advice for this?If you explain what you mean with "distorts time" we can help TTimer is based on milliseconds (1000 ms = 1 s) But in general, TTimer is not that precise like a multimedia timer (on Windows) is. ---------------------------------------------------------------------- Pe3s · 2023-03-23 10:17 I set 20 seconds, the timer counts down from 18, which means it loses 1 second ---------------------------------------------------------------------- KodeZwerg · 2023-03-23 10:45 Since you do not show how you are doing, use my example and document the place where you think it does fail. ---------------------------------------------------------------------- alpine · 2023-03-23 10:48 Quote from: Pe3s on March 23, 2023, 11:17:27 amI set 20 seconds, the timer counts down from 18, which means it loses 1 second In order to sample something (in your case - time in seconds) without distortion, you must sample it with a higher rate than the Nyquist rate. If the highest frequency of the signal is (1/1s=1Hz) then <500ms. Your timer interval must be less than 500. ---------------------------------------------------------------------- Pe3s · 2023-03-23 11:36 Great, both solutions work. Thank you Have a nice day ---------------------------------------------------------------------- alpine · 2023-03-24 08:03 Quote from: Pe3s on March 23, 2023, 12:36:33 pmGreat, both solutions work. Thank you Have a nice day If the accuracy is important, I wouldn't count on a solution which decrements the seconds by itself rather than calculating a delta from the saved timestamp at the beginning. TTimer works with WM_TIMER messages and the docs says they can be coalesced when not processed quickly enough. Also any delay in processing may sum up into a cumulative error. ---------------------------------------------------------------------- KodeZwerg · 2023-03-24 15:47 Quote from: alpine on March 24, 2023, 09:03:52 amQuote from: Pe3s on March 23, 2023, 12:36:33 pmGreat, both solutions work. Thank you Have a nice day If the accuracy is important, I wouldn't count on a solution which decrements the seconds by itself rather than calculating a delta from the saved timestamp at the beginning. TTimer works with WM_TIMER messages and the docs says they can be coalesced when not processed quickly enough. Also any delay in processing may sum up into a cumulative error. If you mean my example, I decrement in one timer each second a value to display but other timer (the real) is not harmed by it ...