Microsoft KB Archive/167169

= FIX: COleDateTime::Format Throws a COleException(scode=E_FAIL) =

Article ID: 167169

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 4.0 Standard Edition

 Microsoft Visual C++ 4.0 Standard Edition

 Microsoft Visual C++ 4.1 Subscription

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q167169

<div class="symptoms_section">

SYMPTOMS
When you attempt to use COleDateTime(DWORD, LCID) to extract the Date or Time value, an exception with scode = E_FAIL occurs.

<div class="cause_section">

CAUSE
A bug in the Operating System causes this problem.

<div class="resolution_section">

RESOLUTION
This 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.

<div class="status_section">

STATUS
This problem was corrected in Visual C++ version 6.0 for Windows.

<div class="moreinformation_section">

Sample Code
This 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
<ol> In the Control Panel, double-click the Regional Settings icon to open the Regional Setting dialog box.</li> In the Regional Settings dialog box, select a country other than English (United States) in the Regional Settings tab.</li> Create a default MFC AppWizard application.</li>  Add the following code to any function: COleDateTime today = COleDateTime::GetCurrentTime; CString m_Date = today.Format(VAR_DATEVALUEONLY); TRACE("Date : %s\n", (LPCTSTR)m_Date); </li> Set a breakpoint on the function above and you will see that this throws a COleException error with an SCODE of E_FAIL($80004005).</li></ol>

Additional query words: COleDateTime Format mfcole

Keywords: kbbug kbfix kbnoupdate kbvc600fix kbcode KB167169

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.