SNTPBox.pas 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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: 11275: SNTPBox.pas
  11. {
  12. { Rev 1.0 11/12/2002 09:19:46 PM JPMugaas
  13. { Initial check in. Import from FTP VC.
  14. }
  15. unit SNTPBox;
  16. interface
  17. uses
  18. IndyBox,
  19. IdSNTP;
  20. type
  21. TSNTPBox = class(TIndyBox)
  22. protected
  23. FClient: TIdSNTP;
  24. public
  25. procedure Test; override;
  26. procedure DoGetTime;
  27. procedure DoSetTime;
  28. end;
  29. implementation
  30. uses
  31. Windows,
  32. SysUtils,
  33. IniFiles;
  34. const
  35. {
  36. SNTP hosts:
  37. clock.psu.edu clock.isc.org time.twc.weather.com tick.uh.edu tick.mhpcc.edu
  38. }
  39. GTimeHost = 'clock.psu.edu';
  40. GTimeout = 5000;
  41. procedure TSNTPBox.Test;
  42. begin
  43. FClient := TIdSNTP.Create(Nil);
  44. FClient.Host := Trim(GlobalParamValue('SNTP Server'));
  45. if Length(FClient.Host) = 0 then
  46. begin
  47. FClient.Host := GTimeHost;
  48. end;
  49. FClient.ReceiveTimeout := GTimeout;
  50. try
  51. Status('SNTP Client Tests (2)');
  52. DoGetTime;
  53. DoSetTime;
  54. Status('SNTP Client Tests... Done');
  55. finally
  56. FreeAndNil(FClient);
  57. end;
  58. end;
  59. procedure TSNTPBox.DoGetTime;
  60. var
  61. iCounter: integer;
  62. iCount: integer;
  63. sMsgForm: string;
  64. begin
  65. iCount := 10;
  66. sMsgForm := '[%d] GetTime - Time %s RoundTrip %s Adjustment %s';
  67. Status(Format('SNTP Client Test (1) GetTime [1..%d]', [ iCount ]));
  68. for iCounter := 1 to iCount do
  69. begin
  70. Status(Format(sMsgForm,
  71. [ iCounter,
  72. FormatDateTime('hh:nn:ss.zzz', FClient.DateTime),
  73. FormatDateTime('hh:nn:ss.zzz', FClient.RoundTripDelay),
  74. FormatDateTime('hh:nn:ss.zzz', FClient.AdjustmentTime) ] ));
  75. Sleep(GTimeout);
  76. end;
  77. Status('SNTP Client Test (1) GetTime... Done.');
  78. end;
  79. procedure TSNTPBox.DoSetTime;
  80. var
  81. // ASystemTime: TSystemTime;
  82. ADateTime: TDateTime;
  83. // iCounter: integer;
  84. wMo, wDay, wYr: Word;
  85. begin
  86. ADateTime := Now;
  87. DecodeDate(ADateTime, wYr, wMo, wDay);
  88. // ADateTime := EncodeDate(wYr, wMo, wDay) + EncodeTime(0, 0, 0, 1);
  89. Status('SNTP Client Test (2) SetTime');
  90. Status('SetTime - Time on Test entry ' + FormatDateTime('hh:nn:ss.zzz', Now));
  91. // SetLocalTime(DateTimeToSystemTime(ADateTime));
  92. Status('SetTime - Time before Test ' + FormatDateTime('hh:nn:ss.zzz', Now));
  93. FClient.SyncTime;
  94. Status('SetTime - Time after Test ' + FormatDateTime('hh:nn:ss.zzz', Now));
  95. Status('SNTP Client Test (2) SetTime... Done.');
  96. end;
  97. initialization
  98. TIndyBox.RegisterBox(TSNTPBox, 'SNTP Client', 'Clients');
  99. end.