IdTimeServer.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.8 2/10/2005 2:24:42 PM JPMugaas
  18. Minor Restructures for some new UnixTime Service components.
  19. Rev 1.7 12/2/2004 4:24:00 PM JPMugaas
  20. Adjusted for changes in Core.
  21. Rev 1.6 2004.02.03 5:44:34 PM czhower
  22. Name changes
  23. Rev 1.5 1/21/2004 4:20:58 PM JPMugaas
  24. InitComponent
  25. Rev 1.4 2003.10.12 6:36:44 PM czhower
  26. Now compiles.
  27. Rev 1.3 2/24/2003 10:37:04 PM JPMugaas
  28. Should compile. TODO: Figure out what to do with TIdTime and the timeout
  29. feature.
  30. Rev 1.2 1/17/2003 07:11:08 PM JPMugaas
  31. Now compiles under new framework.
  32. Rev 1.1 1/8/2003 05:54:00 PM JPMugaas
  33. Switched stuff to IdContext.
  34. Rev 1.0 11/13/2002 08:03:20 AM JPMugaas
  35. }
  36. unit IdTimeServer;
  37. interface
  38. {$i IdCompilerDefines.inc}
  39. {
  40. 2000-3-May J. Peter Mugaas
  41. -Added BaseDate to the date the calculations are based on can be
  42. adjusted to work after the year 2035
  43. 2000-30-April J. Peter Mugaas
  44. -Adjusted the formula for the integer so that the Time is now
  45. always based on Universal Time (also known as Greenwhich Mean
  46. -Time Replaced the old forumala used to calculate the time with
  47. a new one suggested by Jim Gunkel. This forumala is more
  48. accurate than the old one.
  49. 2000-24-April J. Peter Mugaaas
  50. -This now uses the Internet Byte order functions
  51. 2000-22-Apr J Peter Mugass
  52. -Ported to Indy
  53. -Fixed a problem where the server was not returning anything
  54. 2000-13-Jan MTL
  55. -Moved to new Palette Scheme (Winshoes Servers)
  56. 1999-13-Apr
  57. -Final Version
  58. Original Author: Ozz Nixon
  59. -Based on RFC 868
  60. }
  61. uses
  62. {$IFDEF WORKAROUND_INLINE_CONSTRUCTORS}
  63. Classes,
  64. {$ENDIF}
  65. IdAssignedNumbers,
  66. IdContext,
  67. IdCustomTCPServer;
  68. Type
  69. TIdCustomTimeServer = class(TIdCustomTCPServer)
  70. protected
  71. FBaseDate : TDateTime;
  72. //
  73. function DoExecute(AContext: TIdContext): Boolean; override;
  74. procedure InitComponent; override;
  75. {$IFDEF WORKAROUND_INLINE_CONSTRUCTORS}
  76. public
  77. constructor Create(AOwner: TComponent); reintroduce; overload;
  78. {$ENDIF}
  79. published
  80. end;
  81. TIdTimeServer = class(TIdCustomTimeServer)
  82. published
  83. property DefaultPort default IdPORT_TIME;
  84. {This property is used to set the Date the Time server bases it's
  85. calculations from. If both the server and client are based from the same
  86. date which is higher than the original date, you can extend it beyond the
  87. year 2035}
  88. property BaseDate : TDateTime read FBaseDate write FBaseDate;
  89. end;
  90. implementation
  91. uses
  92. {$IFDEF USE_VCL_POSIX}
  93. Posix.Time,
  94. {$ENDIF}
  95. IdGlobal, //here to facilitate inlining
  96. IdGlobalProtocols,
  97. SysUtils;
  98. {$IFDEF WORKAROUND_INLINE_CONSTRUCTORS}
  99. constructor TIdCustomTimeServer.Create(AOwner: TComponent);
  100. begin
  101. inherited Create(AOwner);
  102. end;
  103. {$ENDIF}
  104. procedure TIdCustomTimeServer.InitComponent;
  105. begin
  106. inherited;
  107. DefaultPort := IdPORT_TIME;
  108. {This indicates that the default date is Jan 1, 1900 which was specified
  109. by RFC 868.}
  110. FBaseDate := TIME_BASEDATE;
  111. end;
  112. function TIdCustomTimeServer.DoExecute(AContext: TIdContext): Boolean;
  113. begin
  114. Result := true;
  115. AContext.Connection.IOHandler.Write(UInt32(Trunc(Extended(LocalTimeToUTCTime(Now) - Int(FBaseDate)) * 24 * 60 * 60)));
  116. AContext.Connection.Disconnect;
  117. end;
  118. end.