Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.4 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. Time.hxx
  5. Abstract:
  6. The CTime class represents time for this process. Instances of this class
  7. should be small 4-8 bytes and cheap to create.
  8. The current implementation is just a wrapper for GetTickCount() which
  9. handles overflow.
  10. (4GB milliseconds is just under 50 days. This implementation is correctly
  11. compare times up to 25 days appart.).
  12. Note:
  13. This class should return consistent results even if the system time
  14. is set forwards or backwards.
  15. (Use GetTickCount() rather then GetSystemTimeAsFileTime()).
  16. Author:
  17. Mario Goertzel [MarioGo]
  18. Revision History:
  19. MarioGo 02-23-95 Bits 'n pieces
  20. --*/
  21. #ifndef __TIME_HXX
  22. #define __TIME_HXX
  23. #define TICKS_PER_SECOND (1000) // Ticks are milliseconds
  24. class CTime
  25. {
  26. private:
  27. DWORD _Time;
  28. public:
  29. CTime() { SetNow(); }
  30. // Used to avoid a call to GetTickCount()
  31. CTime(DWORD time) : _Time(time) { }
  32. void
  33. SetNow() {
  34. _Time = GetTickCount();
  35. }
  36. void
  37. Sleep()
  38. {
  39. DWORD diff = _Time - GetTickCount();
  40. if ( diff > (2 * BaseTimeoutInterval) * 1000 )
  41. {
  42. // Maybe overactive under stress. We're trying to sleep until a time
  43. // which has already passed.
  44. KdPrintEx((DPFLTR_DCOMSS_ID,
  45. DPFLTR_INFO_LEVEL,
  46. "Didn't need to sleep until %d from %d\n",
  47. _Time,
  48. GetTickCount()));
  49. return;
  50. }
  51. SleepEx(diff, FALSE);
  52. }
  53. BOOL operator< (const CTime &Time)
  54. {
  55. // Is _Time less then Time.
  56. DWORD diff = _Time - Time._Time;
  57. return( ((LONG)diff) < 0);
  58. }
  59. BOOL operator> (const CTime &Time)
  60. {
  61. // Is _Time greater then Time.
  62. DWORD diff = Time._Time - _Time;
  63. return( ((LONG)diff) < 0);
  64. }
  65. BOOL operator<= (const CTime &Time)
  66. {
  67. return(! operator>(Time));
  68. }
  69. BOOL operator>= (const CTime &Time)
  70. {
  71. return(! operator<(Time));
  72. }
  73. void operator+=(UINT mSeconds) { _Time += (mSeconds * TICKS_PER_SECOND); }
  74. void operator-=(UINT mSeconds) { _Time -= (mSeconds * TICKS_PER_SECOND); }
  75. DWORD operator-(const CTime &Time)
  76. {
  77. return((_Time - Time._Time)/TICKS_PER_SECOND);
  78. }
  79. // defualt = operator ok.
  80. };
  81. #endif __TIME_HXX