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.

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