Recent

Author Topic: Seconds since...  (Read 4016 times)

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Seconds since...
« on: February 01, 2017, 05:59:40 pm »
I need to pass "current time as seconds since Jan 1 1970,"  to match up with an Arduino Time Library.

Is there an existing Function/Procedure available in FPC?

I found this Delphi stuff online, but FPC is stumbling with ULarge and SystemTimeTo...

I tried converting using Lazarus but it made no changes but still will not compile.
The "SystemTimeToFileTime" does not show up with the F1-Help button.

Thoughts and suggestions please?

Code: [Select]
{code: Delphi}
function AbsNumSecs(ASystemTime: TSystemTime) : Int64;
//returns # of seconds since 00:00:00:000, 01/01/1601
var
   lpFileTime: TFileTime;
   ULISysTime : ULARGE_INTEGER;
begin
   result := -1;
   if SystemTimeToFileTime(ASystemTime,lpFileTime) then begin
     ULISysTime.LowPart := lpFileTime.dwLowDateTime;
     ULISysTime.HighPart := lpFileTime.dwHighDateTime;
     //The TFILETIME structure is a 64-bit value representing
     //the number of 100-nanosecond intervals since 01/01/1601(UTC)
     result := ULISysTime.QuadPart div 10000000;
   end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
   lpSystemTime: TSystemTime;
   NSecs : Int64;
begin
   GetSystemTime(lpSystemTime);
   NSecs := AbsNumSecs(lpSystemTime);
   if (NSecs <> -1)
     then ShowMessage('Seconds since 00:00:00:000 - 01/01/1601:'+
                      IntToStr(NSecs))
     else ShowMessage('Error in AbsNumSecs!'+#13+
                      SysErrorMessage(GetLastError));
end;
{code: Delphi}
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

Bart

  • Hero Member
  • *****
  • Posts: 5743
    • Bart en Mariska's Webstek
Re: Seconds since...
« Reply #1 on: February 01, 2017, 06:02:23 pm »
Function SecondsBetween() from DateUtils unit?

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 19393
  • Glad to be alive.
Re: Seconds since...
« Reply #2 on: February 01, 2017, 06:27:30 pm »
http://www.freepascal.org/docs-html/rtl/sysutils/systemtimetodatetime.html

It should be there ..... Oh, wait, it is there ....
objects are fine constructs. You can even initialize them with constructors.

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: Seconds since...
« Reply #3 on: February 01, 2017, 08:20:53 pm »
It should be there ..... Oh, wait, it is there ....

But -- not when I push the F1-button. See OP.

I must confess I didn't think of looking online docs since I specifically installed the local Help.

Silly me for thinking the "Help" would be the same. :)

{edit} OOoops, still will not compile and SysUtils was already in the Uses statement.

umytools.pas(247,7) Error: Identifier not found "SystemTimeToFileTime"

"FileTime" is the key here
« Last Edit: February 01, 2017, 08:38:11 pm by HatForCat »
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: Seconds since...
« Reply #4 on: February 01, 2017, 08:23:17 pm »
Function SecondsBetween() from DateUtils unit?

Ahh, OK, thanks, that should do fine.

Seems like another frailty of the local Help system. "Seconds" did not show up on the F1-key push.

I thought would have to be something to use though.
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

 

TinyPortal © 2005-2018