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.

168 lines
5.2 KiB

  1. // *********************************************************************************
  2. //
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. //
  5. // Module Name:
  6. //
  7. // DateTimeObject.cpp
  8. //
  9. // Abstract:
  10. //
  11. // This component is required by VB Scripts to get date and time in various calenders.
  12. //
  13. // Author:
  14. //
  15. // Bala Neerumalla ([email protected]) 31-July-2001
  16. //
  17. // Revision History:
  18. //
  19. // Bala Neerumalla ([email protected]) 31-July-2001 : Created It.
  20. //
  21. // *********************************************************************************
  22. #include "pch.h"
  23. #include "ScriptingUtils.h"
  24. #include "DateTimeObject.h"
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CDateTimeObject
  27. // ***************************************************************************
  28. // Routine Description:
  29. // This the entry point to this utility.
  30. //
  31. // Arguments:
  32. // [ in ] bstrInDateTime : argument containing the date and time in
  33. // YYYYMMDDHHMMSS.MMMMMM format
  34. // [ out ] pVarDateTime : argument returning date and time in Locale
  35. // specific format
  36. //
  37. // Return Value:
  38. // This functin returns S_FALSE if any errors occur else returns S_OK.
  39. // ***************************************************************************
  40. STDMETHODIMP CDateTimeObject::GetDateAndTime(BSTR bstrInDateTime, VARIANT *pVarDateTime)
  41. {
  42. DWORD dwCount = 0;
  43. BOOL bLocaleChanged = FALSE;
  44. SYSTEMTIME systime;
  45. CHString strDate,strTime;
  46. LCID lcid;
  47. try
  48. {
  49. lcid = GetSupportedUserLocale(bLocaleChanged);
  50. systime = GetDateTime(bstrInDateTime);
  51. dwCount = GetDateFormat( lcid, 0, &systime,
  52. ((bLocaleChanged == TRUE) ? _T("MM/dd/yyyy") : NULL), NULL, 0 );
  53. // get the required buffer
  54. LPWSTR pwszTemp = NULL;
  55. pwszTemp = strDate.GetBufferSetLength( dwCount + 1 );
  56. // now format the date
  57. GetDateFormat( lcid, 0, &systime,
  58. (LPTSTR)((bLocaleChanged == TRUE) ? _T("MM/dd/yyyy") : NULL), pwszTemp, dwCount );
  59. // release the buffer3
  60. strDate.ReleaseBuffer();
  61. // get the formatted time
  62. // get the size of buffer that is needed
  63. dwCount = 0;
  64. dwCount = GetTimeFormat( lcid, 0, &systime,
  65. ((bLocaleChanged == TRUE) ? L"HH:mm:ss" : NULL), NULL, 0 );
  66. // get the required buffer
  67. pwszTemp = NULL;
  68. pwszTemp = strTime.GetBufferSetLength( dwCount + 1 );
  69. // now format the date
  70. GetTimeFormat( lcid, 0, &systime,
  71. ((bLocaleChanged == TRUE) ? L"HH:mm:ss" : NULL), pwszTemp, dwCount );
  72. // release the buffer
  73. strTime.ReleaseBuffer();
  74. // Initialize the Out Variant.
  75. VariantInit(pVarDateTime);
  76. pVarDateTime->vt = VT_BSTR;
  77. // Put it in the out parameter.
  78. pVarDateTime->bstrVal = SysAllocString((LPCWSTR)(strDate + L" " + strTime));
  79. }
  80. catch( ... )
  81. {
  82. return E_OUTOFMEMORY;
  83. }
  84. return S_OK;
  85. }
  86. // ***************************************************************************
  87. // Routine Description:
  88. // This function returns LCID of the current USer Locale. If the User locale
  89. // is not a supported one, it would return LCID of English language.
  90. //
  91. // Arguments:
  92. // [ out ] bLocaleChanged : argument returning whether the user locale is
  93. // changed or not.
  94. //
  95. // Return Value:
  96. // This function returns LCID of the User Locale.
  97. // ***************************************************************************
  98. LCID CDateTimeObject::GetSupportedUserLocale( BOOL& bLocaleChanged )
  99. {
  100. // local variables
  101. LCID lcid;
  102. // get the current locale
  103. lcid = GetUserDefaultLCID();
  104. // check whether the current locale is supported by our tool or not
  105. // if not change the locale to the english which is our default locale
  106. bLocaleChanged = FALSE;
  107. if ( PRIMARYLANGID( lcid ) == LANG_ARABIC || PRIMARYLANGID( lcid ) == LANG_HEBREW ||
  108. PRIMARYLANGID( lcid ) == LANG_THAI || PRIMARYLANGID( lcid ) == LANG_HINDI ||
  109. PRIMARYLANGID( lcid ) == LANG_TAMIL || PRIMARYLANGID( lcid ) == LANG_FARSI )
  110. {
  111. bLocaleChanged = TRUE;
  112. lcid = MAKELCID( MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT ), SORT_DEFAULT ); // 0x409;
  113. }
  114. // return the locale
  115. return lcid;
  116. }
  117. // ***************************************************************************
  118. // Routine Description:
  119. // This function extracts the date and time fields from a string.
  120. //
  121. // Arguments:
  122. // [ in ] strTime : string containing the date and time in the format
  123. // YYYYMMDDHHMMSS.MMMMMM.
  124. //
  125. // Return Value:
  126. // returns SYSTEMTIME structure containing the date & time info present in
  127. // strTime.
  128. // ***************************************************************************
  129. SYSTEMTIME CDateTimeObject::GetDateTime(CHString strTime)
  130. {
  131. SYSTEMTIME systime;
  132. systime.wYear = (WORD) _ttoi( strTime.Left( 4 ));
  133. systime.wMonth = (WORD) _ttoi( strTime.Mid( 4, 2 ));
  134. systime.wDayOfWeek = 0;
  135. systime.wDay = (WORD) _ttoi( strTime.Mid( 6, 2 ));
  136. systime.wHour = (WORD) _ttoi( strTime.Mid( 8, 2 ));
  137. systime.wMinute = (WORD) _ttoi( strTime.Mid( 10, 2 ));
  138. systime.wSecond = (WORD) _ttoi( strTime.Mid( 12, 2 ));
  139. systime.wMilliseconds = (WORD) _ttoi( strTime.Mid( 15, 6 ));
  140. return systime;
  141. }