Delphi DLL
Delphi DLL
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 Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, CkDateTime, JsonObject;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
json: HCkJsonObject;
dt: HCkDateTime;
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 := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'ntp_server','pool.ntp.org');
// Query the NTP server for the current date/time.
dt := CkDateTime_Create();
success := CkDateTime_SetFromNtpServer(dt,CkJsonObject__emit(json));
if (success = False) then
begin
Memo1.Lines.Add(CkDateTime__lastErrorText(dt));
Exit;
end;
// Show the date/time in various formats..
bLocalTime := True;
Memo1.Lines.Add(CkDateTime__getAsTimestamp(dt,bLocalTime));
Memo1.Lines.Add(CkDateTime__getAsRfc822(dt,bLocalTime));
Memo1.Lines.Add(IntToStr(CkDateTime_GetAsUnixTime(dt,bLocalTime)));
// Sample output:
// 2023-10-31T09:43:27-05:00
// Tue, 31 Oct 2023 09:43:27 -0500
// 1698745407
CkJsonObject_Dispose(json);
CkDateTime_Dispose(dt);
end;