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.

161 lines
4.1 KiB

  1. //******************************************************************************
  2. //
  3. // Microsoft Confidential. Copyright (c) Microsoft Corporation 1999. All rights reserved
  4. //
  5. // File: WbemTime.cpp
  6. //
  7. // Description: Utility functions to convert between SYSTEMTIME and strings in
  8. // WBEM datetime format.
  9. //
  10. // History: 12-08-99 leonardm Created
  11. //
  12. //******************************************************************************
  13. #include <wchar.h>
  14. #include "smartptr.h"
  15. #include "WbemTime.h"
  16. #include <strsafe.h>
  17. //******************************************************************************
  18. //
  19. // Function: SystemTimeToWbemTime
  20. //
  21. // Description:
  22. //
  23. // Parameters:
  24. //
  25. // Return:
  26. //
  27. // History: 12/08/99 leonardm Created.
  28. //
  29. //******************************************************************************
  30. HRESULT SystemTimeToWbemTime(SYSTEMTIME& sysTime, XBStr& xbstrWbemTime)
  31. {
  32. DWORD dwTempLength = WBEM_TIME_STRING_LENGTH + 1;
  33. XPtrST<WCHAR> xTemp = new WCHAR[dwTempLength];
  34. if(!xTemp)
  35. {
  36. return E_OUTOFMEMORY;
  37. }
  38. HRESULT hr = StringCchPrintf(xTemp,
  39. dwTempLength,
  40. L"%04d%02d%02d%02d%02d%02d.000000+000",
  41. sysTime.wYear,
  42. sysTime.wMonth,
  43. sysTime.wDay,
  44. sysTime.wHour,
  45. sysTime.wMinute,
  46. sysTime.wSecond);
  47. if(FAILED(hr))
  48. {
  49. return E_FAIL;
  50. }
  51. xbstrWbemTime = xTemp;
  52. if(!xbstrWbemTime)
  53. {
  54. return E_OUTOFMEMORY;
  55. }
  56. return S_OK;
  57. }
  58. //******************************************************************************
  59. //
  60. // Function: WbemTimeToSystemTime
  61. //
  62. // Description:
  63. //
  64. // Parameters:
  65. //
  66. // Return:
  67. //
  68. // History: 12/08/99 leonardm Created.
  69. //
  70. //******************************************************************************
  71. HRESULT WbemTimeToSystemTime(XBStr& xbstrWbemTime, SYSTEMTIME& sysTime)
  72. {
  73. if(!xbstrWbemTime || wcslen(xbstrWbemTime) != WBEM_TIME_STRING_LENGTH)
  74. {
  75. return ERROR_INVALID_PARAMETER;
  76. }
  77. for(int i = 0; i < 14; i++)
  78. {
  79. if(!iswdigit(xbstrWbemTime[i]))
  80. {
  81. return ERROR_INVALID_PARAMETER;
  82. }
  83. }
  84. XPtrST<WCHAR>xpTemp = new WCHAR[5];
  85. if(!xpTemp)
  86. {
  87. return E_OUTOFMEMORY;
  88. }
  89. wcsncpy(xpTemp, xbstrWbemTime, 4);
  90. xpTemp[4] = L'\0';
  91. sysTime.wYear = (WORD)_wtol(xpTemp);
  92. wcsncpy(xpTemp, xbstrWbemTime + 4, 2);
  93. xpTemp[2] = L'\0';
  94. sysTime.wMonth = (WORD)_wtol(xpTemp);
  95. wcsncpy(xpTemp, xbstrWbemTime + 6, 2);
  96. xpTemp[2] = L'\0';
  97. sysTime.wDay = (WORD)_wtol(xpTemp);
  98. wcsncpy(xpTemp, xbstrWbemTime + 8, 2);
  99. xpTemp[2] = L'\0';
  100. sysTime.wHour = (WORD)_wtol(xpTemp);
  101. wcsncpy(xpTemp, xbstrWbemTime + 10, 2);
  102. xpTemp[2] = L'\0';
  103. sysTime.wMinute = (WORD)_wtol(xpTemp);
  104. wcsncpy(xpTemp, xbstrWbemTime + 12, 2);
  105. xpTemp[2] = L'\0';
  106. sysTime.wSecond = (WORD)_wtol(xpTemp);
  107. sysTime.wMilliseconds = 0;
  108. sysTime.wDayOfWeek = 0;
  109. return S_OK;
  110. }
  111. //*************************************************************
  112. //
  113. // Function: GetCurrentWbemTime
  114. //
  115. // Purpose: Gets the current date and time in WBEM format.
  116. //
  117. // Parameters: xbstrCurrentTime - Reference to XBStr which, on
  118. // success, receives the formated
  119. // string containing the current
  120. // date and time.
  121. //
  122. // Returns: On success it returns S_OK.
  123. // On failure, it returns E_OUTOFMEMORY.
  124. //
  125. // History: 12/07/99 - LeonardM - Created.
  126. //
  127. //*************************************************************
  128. HRESULT GetCurrentWbemTime(XBStr& xbstrCurrentTime)
  129. {
  130. SYSTEMTIME sytemTime;
  131. GetSystemTime(&sytemTime);
  132. HRESULT hr = SystemTimeToWbemTime(sytemTime, xbstrCurrentTime);
  133. return hr;
  134. }