Recent

Author Topic: [SOLVED] Representing seconds in TMaskEdit  (Read 1292 times)

Zvoni

  • Hero Member
  • *****
  • Posts: 3300
[SOLVED] Representing seconds in TMaskEdit
« on: August 31, 2020, 07:14:19 pm »
Hi Folks,

either im completely stupid, or i just can't see the solution (I'm guessing the first....):
I'm getting a Duration of a process in seconds from a File as a Double/DateTime
e.g.: 90.16 ("Ninety Point one six seconds")

How do i get a TMaskEdit to show: 01:30:160 ("one minute, thirty seconds, and change....")

Completely stumped......
« Last Edit: August 31, 2020, 08:28:36 pm by Zvoni »
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

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Representing seconds in TMaskEdit
« Reply #1 on: August 31, 2020, 07:25:35 pm »
either im completely stupid, or i just can't see the solution (I'm guessing the first....):
Then let's try a KISS approach  :)

Assuming the part after the dot is based on tenths (decimal fraction), multiply the value by 1000 to get the duration in milliseconds (make sure to make it an integer), set a TDateTimeValue to 0.0 and use DateUtils incmillisecond function to add the number of milliseconds to that time, see https://www.freepascal.org/docs-html/rtl/dateutils/incmillisecond.html

Quote
How do i get a TMaskEdit to show: 01:30:160 ("one minute, thirty seconds, and change....")
You then could take the DateTime value and convert it into a string using FormatDateTime, see https://www.freepascal.org/docs-html/rtl/sysutils/formatdatetime.html, to represent the value as a string. Note the format characters, see also https://www.freepascal.org/docs-html/rtl/sysutils/formatchars.html

Or you could use one of the many other datetime related routines to extract particular parts from that datetime value (for instance decodetime, https://www.freepascal.org/docs-html/rtl/sysutils/decodetime.html).

How you would integrated that into your maskedit... I have no idea  :-[

edit: typo's
« Last Edit: August 31, 2020, 07:29:03 pm by TRon »
Today is tomorrow's yesterday.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Representing seconds in TMaskEdit
« Reply #2 on: August 31, 2020, 08:14:28 pm »
Hi

First convert your 90.16 seconds relativ to the whole day for the TDateTime format:

Code: Pascal  [Select][+][-]
  1. myTime := 90.16 / SecsPerDay;


THen get the simple daily format for a string with TimeToStr: hh:mm:ss
Or get the format that you need with formatDateTime.

Finaly separate the TDateTime into 4 words: hour,min,sec,milliSec.

That's the whole secret.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. const theSecs = 90.16;
  3. var myTime : TDateTime;
  4.    hour,min,sec,milli: word;
  5.  
  6. begin
  7. myTime := TheSecs/SecsPerDay;
  8. showMessage('Imprecise: '+TimeToStr(myTime));
  9. showMessage('Precise: '+FormatDateTime('hh:nn:ss.zzz',myTime));
  10. DecodeTime(myTime,hour,min,sec,milli);
  11. end;

Winni
« Last Edit: August 31, 2020, 08:16:26 pm by winni »

Zvoni

  • Hero Member
  • *****
  • Posts: 3300
Re: Representing seconds in TMaskEdit
« Reply #3 on: August 31, 2020, 08:22:22 pm »
Winni,
you saved my life.
I knew it was something KISS (--> keep it simple stupid);
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

 

TinyPortal © 2005-2018