TimeSyncMain.pas 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 115832: TimeSyncMain.pas }
  11. {
  12. Rev 1.0 2/11/2005 1:52:24 AM DSiders
  13. Initial checkin.
  14. }
  15. unit TimeSyncMain;
  16. interface
  17. uses
  18. Windows, Messages, SysUtils, Classes, Forms, Dialogs,
  19. Graphics, Controls, StdCtrls, ExtCtrls,
  20. IdBaseComponent, IdComponent, IdUDPBase, IdUDPClient, IdSNTP;
  21. type
  22. TFormTimeSync = class(TForm)
  23. PanelInfo: TPanel;
  24. ImageTimeSync: TImage;
  25. LabelInfo: TLabel;
  26. LabelSNTPHost: TLabel;
  27. LabelTimeout: TLabel;
  28. LabelHostTime: TLabel;
  29. LabelTimeZone: TLabel;
  30. LabelLocalTime: TLabel;
  31. LabelRoundtrip: TLabel;
  32. LabelOffset: TLabel;
  33. ComboBoxSNTPHost: TComboBox;
  34. EditTimeout: TEdit;
  35. EditHostTime: TEdit;
  36. EditTimezone: TEdit;
  37. EditLocalTime: TEdit;
  38. EditLocalOffset: TEdit;
  39. EditRoundTrip: TEdit;
  40. ButtonGetTime: TButton;
  41. ButtonSetTime: TButton;
  42. SntpClient: TIdSNTP;
  43. procedure ButtonGetTimeClick(Sender: TObject);
  44. procedure ButtonSetTimeClick(Sender: TObject);
  45. procedure ComboBoxSNTPHostChange(Sender: TObject);
  46. procedure UpdateDisplayInfo(const ANow, ADate, ARoundtrip, AOffset: TDateTime);
  47. procedure ClearDisplayInfo;
  48. procedure FormCreate(Sender: TObject);
  49. private
  50. { Private declarations }
  51. public
  52. { Public declarations }
  53. end;
  54. var
  55. FormTimeSync: TFormTimeSync;
  56. implementation
  57. uses
  58. IdGlobal;
  59. {$R *.DFM}
  60. const
  61. csTimeFormat = 'HH:NN:SS.ZZZ';
  62. csCaptionFormat = '%s using the %s SNTP client (%s)';
  63. ciDefaultTimeout = 15000; // in millisecs
  64. procedure TFormTimeSync.ButtonGetTimeClick(Sender: TObject);
  65. var
  66. ADateTime: TDateTime;
  67. ANow: TDateTime;
  68. begin
  69. ClearDisplayInfo;
  70. if ComboBoxSNTPHost.Text <> '' then
  71. SntpClient.Host := ComboBoxSNTPHost.Text;
  72. SntpClient.ReceiveTimeout := StrToIntDef(EditTimeout.Text, ciDefaultTimeout);
  73. try
  74. ANow := Now;
  75. ADateTime := SntpClient.DateTime;
  76. UpdateDisplayInfo(ANow, ADateTime,
  77. SntpClient.RoundTripDelay, SntpClient.AdjustmentTime);
  78. except
  79. on E: Exception do
  80. begin
  81. MessageBeep(MB_ICONEXCLAMATION);
  82. ShowMessage(E.Message)
  83. end;
  84. end;
  85. end;
  86. procedure TFormTimeSync.ButtonSetTimeClick(Sender: TObject);
  87. var
  88. ANow: TDateTime;
  89. begin
  90. ClearDisplayInfo;
  91. if ComboBoxSNTPHost.Text <> '' then
  92. SntpClient.Host := ComboBoxSNTPHost.Text;
  93. SntpClient.ReceiveTimeout := StrToIntDef(EditTimeout.Text, ciDefaultTimeout);
  94. try
  95. ANow := Now;
  96. SntpClient.SyncTime;
  97. UpdateDisplayInfo(ANow, SntpClient.DateTime,
  98. SntpClient.RoundTripDelay, SntpClient.AdjustmentTime);
  99. except
  100. on E: Exception do
  101. begin
  102. MessageBeep(MB_ICONEXCLAMATION);
  103. ShowMessage(E.Message)
  104. end;
  105. end;
  106. end;
  107. procedure TFormTimeSync.ClearDisplayInfo;
  108. begin
  109. EditHostTime.Text := '';
  110. EditRoundTrip.Text := '';
  111. EditLocalOffset.Text := '';
  112. EditTimezone.Text := '';
  113. EditLocalTime.Text := '';
  114. Application.ProcessMessages;
  115. end;
  116. procedure TFormTimeSync.ComboBoxSNTPHostChange(Sender: TObject);
  117. begin
  118. ClearDisplayInfo;
  119. end;
  120. procedure TFormTimeSync.UpdateDisplayInfo(const ANow, ADate, ARoundtrip, AOffset: TDateTime);
  121. var
  122. ATZInfo: _TIME_ZONE_INFORMATION;
  123. begin
  124. EditHostTime.Text := FormatDateTime(csTimeFormat, ADate);
  125. EditRoundTrip.Text := FormatDateTime(csTimeFormat, ARoundtrip);
  126. EditLocalOffset.Text := FormatDateTime(csTimeFormat, AOffset);
  127. EditLocalTime.Text := FormatDateTime(csTimeFormat, ANow);
  128. GetTimeZoneInformation(ATZInfo);
  129. EditTimezone.Text := iif(
  130. ATZInfo.Bias = ATZInfo.StandardBias,
  131. ATZInfo.StandardName,
  132. ATZInfo.DaylightName);
  133. Application.ProcessMessages;
  134. end;
  135. procedure TFormTimeSync.FormCreate(Sender: TObject);
  136. begin
  137. LabelInfo.Caption := Format(csCaptionFormat,
  138. [ Application.Title, gsIdProductName, gsIdVersion ]);
  139. // SNTPHosts.txt has a list of SNTP hosts by country / province
  140. ComboBoxSNTPHost.Items.LoadFromFile('SNTPHost.txt');
  141. if ComboBoxSNTPHost.Items.Count <> 0 then
  142. ComboBoxSNTPHost.ItemIndex := 0;
  143. end;
  144. end.