CTime::Format() returns a wrong date if the CTime object is NULL when the time zone is set to Eastern (US and Canada only) in Visual C++ .NET
Article ID: 190471
Article Last Modified on 1/11/2006
APPLIES TO
- Microsoft Visual C++ 5.0 Enterprise Edition
- Microsoft Visual C++ 6.0 Enterprise Edition
- Microsoft Visual C++ 5.0 Professional Edition
- Microsoft Visual C++ 6.0 Professional Edition
- Microsoft Visual C++ 6.0 Standard Edition
- Microsoft Visual C++ .NET 2003 Standard Edition
- Microsoft Visual C++ .NET 2002 Standard Edition
This article was previously published under Q190471
SYMPTOMS
The string 01/01/1970 or its equivalent, depending on the
time zone, is returned by CTime::Format() when the CTime object is NULL. It is
expected to return a blank string when the CTime object is NULL.
CAUSE
CTime only measures dates after 12:00 a.m. 01/01/1970 GMT.
CTime::Format() makes a call to localtime(), which is expected to
return a blank string when the date is null or invalid. Instead, it returns the
date 12:00 a.m. 01/01/1970 GMT sufficiently compensated with respect to the
time zone settings of the computer.
For example, if the time zone is
set to Eastern (US and Canada only), then the date returned is 12/31/69 because
it is 5 hours behind GMT.
RESOLUTION
Insert an "If condition" to check whether the CTime object
is NULL. If it is, then do not make a call to CTime::Format(). For example:
CTime newVar;
// Other code to manipulate CTime
if (newVar == NULL)
cout << "Date: 'Date is Blank'" << endl;
else
cout << "Date:" << newVar.Format("%m/%d/%y")<<endl;
// Remaining code
STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section. We are researching this bug
and will post new information here in the Microsoft Knowledge Base as it
becomes available.
Additional query words: kbvc500bug kbVC600bug kbDatabase kbMFC
Keywords: kbtshoot kbprb kbpending KB190471