Convert (fractions of) seconds to TDateTime

General discussion about anything related to Ultibo.
mark
Posts: 1277
Joined: Mon Oct 03, 2016 2:12 am
Location: Indianapolis, US

Convert (fractions of) seconds to TDateTime

Postby mark » Sun May 13, 2018 3:38 am

I'd like to do something similar to this, but starting with the UpTime being just the total number of seconds:

Code: Select all

 WorkTime:=SystemFileTimeToDateTime(UpTime);
 AddContent(AResponse,'Up ' + IntToStr(Trunc(WorkTime)) + ' days ' + TimeToStr(WorkTime));

Thanks,
Mark
Ultibo - The Internet of Things that Just Work
User avatar
Ultibo
Site Admin
Posts: 2079
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: Convert (fractions of) seconds to TDateTime

Postby Ultibo » Sun May 13, 2018 11:14 am

mark wrote:I'd like to do something similar to this, but starting with the UpTime being just the total number of seconds:

Code: Select all

 WorkTime:=SystemFileTimeToDateTime(UpTime);
 AddContent(AResponse,'Up ' + IntToStr(Trunc(WorkTime)) + ' days ' + TimeToStr(WorkTime));

Thanks,
Mark

Hi Mark,

I've reread your question several times but I am still not sure what the exact question is that you are asking.

There are a huge range of time functions available but to avoid guessing could you offer a little more clarification?

Thanks

Garry.
Ultibo.org | Make something amazing
https://ultibo.org
mark
Posts: 1277
Joined: Mon Oct 03, 2016 2:12 am
Location: Indianapolis, US

Re: Convert (fractions of) seconds to TDateTime

Postby mark » Mon May 14, 2018 9:37 am

Ultibo wrote:
mark wrote:I'd like to do something similar to this, but starting with the UpTime being just the total number of seconds:

Code: Select all

 WorkTime:=SystemFileTimeToDateTime(UpTime);
 AddContent(AResponse,'Up ' + IntToStr(Trunc(WorkTime)) + ' days ' + TimeToStr(WorkTime));

Thanks,
Mark

Hi Mark,

I've reread your question several times but I am still not sure what the exact question is that you are asking.

There are a huge range of time functions available but to avoid guessing could you offer a little more clarification?

Thanks

Garry.
I have a time in tenths of a second, and I want to convert it to something that can be processed by TimeToStr.
Ultibo - The Internet of Things that Just Work
User avatar
Ultibo
Site Admin
Posts: 2079
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: Convert (fractions of) seconds to TDateTime

Postby Ultibo » Tue May 15, 2018 12:07 am

mark wrote:I have a time in tenths of a second, and I want to convert it to something that can be processed by TimeToStr.

Thanks, that makes sense now.

The TDateTime format in Free Pascal (and Delphi) is a Double (a floating point value) with the integer part representing the date (as a number of days) and the fractional part representing the time.

Taking your case of tenths of a second, if you had a value of 150 that represents 15 seconds you would do something like this to convert it to a TDateTime for passing to TimeToStr().

Code: Select all

var
 Seconds:LongWord;
 DateTime:TDateTime;

begin
  //Convert tenths of a second to seconds
  Seconds:=150 div 10;
 
  //Convert seconds to a TDateTime using the PASCAL_TIME_SECONDS_PER_DAY constant from GlobalConst
  //(This is simply the number of seconds per day which is 86400 or 60 x 60 x 24)
  DateTime:=((Seconds mod PASCAL_TIME_SECONDS_PER_DAY) / PASCAL_TIME_SECONDS_PER_DAY);
 
  //Convert to a string
  Value:=TimeToStr(DateTime);
 

And that would return 00:00:15

If your seconds value can contain more the one day then you can expand that slightly to save the number of days as well, like this:

Code: Select all

  //Convert seconds to TDateTime and store the number of whole days in the integer part
  DateTime:=(Seconds div PASCAL_TIME_SECONDS_PER_DAY) + ((Seconds mod PASCAL_TIME_SECONDS_PER_DAY) / PASCAL_TIME_SECONDS_PER_DAY);
 
  //The number of days can then be extracted using Trunc()
  Days:=Trunc(DateTime);

You can also shortcut and convert directly from tenths of a second but I'll leave that to you to try.
Ultibo.org | Make something amazing
https://ultibo.org

Return to “Discussion”

Who is online

Users browsing this forum: No registered users and 2 guests