Sample code for 30+ languages & platforms
Lazarus Pascal

Query NTP Server for Current Date/Time

Demonstrates how to query an NTP server to get the current date/time.

Note: This feature was added in Chilkat v9.5.0.96.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.CkDateTime,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  json: TJsonObject;
  dt: TCkDateTime;
  bLocalTime: Boolean;

begin
  success := False;

  //  Specify the NTP server domain.
  //  Information is passed in a JSON object to allow for any unanticipated future optional information that might need to be provided.
  json := TJsonObject.Create;
  json.UpdateString('ntp_server','pool.ntp.org');

  //  Query the NTP server for the current date/time.
  dt := TCkDateTime.Create;
  success := dt.SetFromNtpServer(json.Emit());
  if (success = False) then
    begin
      WriteLn(dt.LastErrorText);
      Exit;
    end;

  //  Show the date/time in various formats..
  bLocalTime := True;
  WriteLn(dt.GetAsTimestamp(bLocalTime));
  WriteLn(dt.GetAsRfc822(bLocalTime));
  WriteLn(dt.GetAsUnixTime(bLocalTime));

  //  Sample output:

  //  2023-10-31T09:43:27-05:00
  //  Tue, 31 Oct 2023 09:43:27 -0500
  //  1698745407


  json.Free;
  dt.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.