Leaked source code of windows server 2003
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.

117 lines
2.6 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) Microsoft Corporation
  4. //
  5. // SYNOPSIS
  6. //
  7. // Defines the class TimeOfDay and functions for manipulating hour maps.
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include "ias.h"
  11. #include "TimeOfDay.h"
  12. bool IsHourSet(
  13. const SYSTEMTIME& now,
  14. const BYTE* hourMap
  15. ) throw ()
  16. {
  17. // Compute the byte and bit for the current hour.
  18. size_t hourOfWeek = (now.wDayOfWeek * 24) + now.wHour;
  19. size_t currentByte = hourOfWeek / 8;
  20. BYTE currentBit = 0x80 >> (hourOfWeek % 8);
  21. return (hourMap[currentByte] & currentBit) != 0;
  22. }
  23. DWORD ComputeTimeout(
  24. const SYSTEMTIME& now,
  25. const BYTE* hourMap
  26. ) throw ()
  27. {
  28. const size_t hoursPerWeek = 7 * 24;
  29. // Compute the index of the current hour (our starting point).
  30. size_t idx = (now.wDayOfWeek * 24) + now.wHour;
  31. // Number of hours until we hit an unset bit.
  32. size_t lastHour = 0;
  33. // Search up to one week for an unset bit.
  34. while (lastHour < hoursPerWeek)
  35. {
  36. // Test the corresponding bit.
  37. if ((hourMap[idx / 8] & (0x1 << (idx % 8))) == 0)
  38. {
  39. break;
  40. }
  41. ++lastHour;
  42. ++idx;
  43. // Wrap around if necessary.
  44. if (idx == hoursPerWeek)
  45. {
  46. idx = 0;
  47. }
  48. }
  49. DWORD secondsLeft;
  50. if (lastHour == hoursPerWeek)
  51. {
  52. // All bits were set, so timeout is infinite.
  53. secondsLeft = 0xFFFFFFFF;
  54. }
  55. else if (lastHour > 0)
  56. {
  57. secondsLeft = (lastHour - 1) * 3600;
  58. secondsLeft += (59 - now.wMinute) * 60;
  59. secondsLeft += (60 - now.wSecond);
  60. }
  61. else
  62. {
  63. // First bit was unset, so access denied.
  64. secondsLeft = 0;
  65. }
  66. return secondsLeft;
  67. }
  68. STDMETHODIMP TimeOfDay::IsTrue(IRequest*, VARIANT_BOOL *pVal)
  69. {
  70. _ASSERT(pVal != 0);
  71. SYSTEMTIME now;
  72. GetLocalTime(&now);
  73. *pVal = IsHourSet(now, hourMap) ? VARIANT_TRUE : VARIANT_FALSE;
  74. return S_OK;
  75. }
  76. STDMETHODIMP TimeOfDay::put_ConditionText(BSTR newVal)
  77. {
  78. // Convert the string to an hour map.
  79. BYTE tempMap[IAS_HOUR_MAP_LENGTH];
  80. DWORD dw = IASHourMapFromText(newVal, FALSE, tempMap);
  81. if (dw != NO_ERROR)
  82. {
  83. return HRESULT_FROM_WIN32(dw);
  84. }
  85. // Save the text.
  86. HRESULT hr = Condition::put_ConditionText(newVal);
  87. // Save the hour map.
  88. if (SUCCEEDED(hr))
  89. {
  90. memcpy(hourMap, tempMap, sizeof(hourMap));
  91. }
  92. return hr;
  93. }