PRB: COleDateTime::Format Throws a COleException(scode=E_FAIL)Last reviewed: August 7, 1997Article ID: Q167169 |
The information in this article applies to:
SYMPTOMSWhen you attempt to use COleDateTime(DWORD, LCID) to extract the Date or Time value, an exception with scode = E_FAIL occurs.
CAUSEA bug in the Operating System causes this problem.
RESOLUTIONThis problem only occurs if the LCID argument in the Format function is set to anything other than English (United States), or if the Regional Settings in WinNT/Win95 Control Panel is set to something other than English (United States). You can workaround the problem by using the "%x" or "%X" formatting codes. The "%x" gives the Date representation and "%X" gives the Time representation. For example:
CString str = dt.Format("%x");-or-
CString str = dt.Format("%X");However, they only give the Date format in the US style. To get the date in the user-default locale, use the code in the MORE INFORMATION section of this article. Wrap the call to Format as shown below:
... #include "locales.h" ... CString str; { CSetLocale l(LC_TIME); str = dt.Format("%x"); str = dt.Format("%X"); } ...If you want the date in a particular LCID, then use the following to construct the CSetLocale object:
LCID LCIDVALUE = <set it to the required value>; CSetLocale l(LC_TIME, LCIDVALUE);This code makes sure that the runtime locale is set to the correct locale before the call to Format is made. The locale is reset to the previous locale once the CSetLocale object is destroyed.
STATUSMicrosoft is researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.
MORE INFORMATION
Sample CodeThis sample code for class CSetLocale can be used to set the runtime locale to the user-default locale or to any other locale using LCID. The class resets the runtime locale back to the original locale when the object is destroyed. The first argument to the constructor takes the same values as the category argument to setlocale function. The second argument is any valid LCID.
// Locales.h: interface for the CSetLocale class. // ////////////////////////////////////////////////////////////////////// #if \ !defined(AFX_LOCALES_H__C6C7DD22_B292_11D0_89B4_00AA00B92B2E__INCLUDED_) #define AFX_LOCALES_H__C6C7DD22_B292_11D0_89B4_00AA00B92B2E__INCLUDED_ #include <locale.h> #if _MSC_VER >= 1000 #pragma once #endif // _MSC_VER >= 1000 class CSetLocale { private: int m_category; CString m_lastLocale; public: CSetLocale(int category = LC_ALL, LCID lcid = LOCALE_USER_DEFAULT); virtual ~CSetLocale(); }; #endif // Locales.cpp: implementation of the CSetLocale class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Locales.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CSetLocale::CSetLocale(int category, LCID lcid) : m_category(category) { TCHAR* buf; CString locale; m_lastLocale = CString(_tsetlocale(m_category, NULL)); // get the language int len = GetLocaleInfo(lcid, LOCALE_SENGLANGUAGE , NULL, 0); buf = new TCHAR[len + 1]; if (!buf) return; buf[len] = NULL; len = GetLocaleInfo(lcid, LOCALE_SENGLANGUAGE , buf, len); locale = CString(buf) + _T("_"); delete []buf; // Get the country. len = GetLocaleInfo(lcid, LOCALE_SENGCOUNTRY, NULL, 0); buf = new TCHAR[len + 1]; if (!buf) return; buf[len] = NULL; len = GetLocaleInfo(lcid, LOCALE_SENGCOUNTRY, buf, len); locale +=buf; delete []buf; _tsetlocale(m_category, locale); } CSetLocale::~CSetLocale() { _tsetlocale(m_category, m_lastLocale); } Steps to Reproduce Problem
|
Additional query words: COleDateTime Format
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |