Microsoft KB Archive/241718

= How To Convert a DBTIMESTAMP into a DATE =

Article ID: 241718

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 4.2 Enterprise Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 4.2 Professional Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition

-



This article was previously published under Q241718



SUMMARY
When given a DBTIMESTAMP value, you might need to convert the value into a type that is compatible with COM IDispatch interfaces, such as the DATE type. If you are using Microsoft Foundation Classes (MFC), you can use the COleDateTime class to help convert from a DBTIMESTAMP to a DATE. If you are not using MFC, you can still use the code provided by MFC without needing the entire MFC framework.



If You Are Using MFC, Use this Code

 * 1) include 

void MFCTestConversion {  DBTIMESTAMP dbTimeStamp;

dbTimeStamp.year = 1900; dbTimeStamp.month = 1; dbTimeStamp.day = 1; dbTimeStamp.hour = 0; dbTimeStamp.minute = 0; dbTimeStamp.second = 0;

COleDateTime d(dbTimeStamp.year, dbTimeStamp.month, dbTimeStamp.day,             dbTimeStamp.hour, dbTimeStamp.minute, dbTimeStamp.second);

COleVariant var = d;  DATE date = var.date; }

If You Are Not Using MFC, Use this Code
//headers needed
 * 1) include 
 * 2) include 

// One-based array of days in year at month start int MonthDays[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};

// Modified Helper taken from \MFC\SRC\OLEVAR.CPP, _AfxOleDateFromTm // This will compile in .c files too BOOL OleDateFromTm(WORD wYear, WORD wMonth, WORD wDay,                   WORD wHour, WORD wMinute, WORD wSecond, DATE *dtDest) {  long nDate; double dblTime; BOOL bLeapYear; int nDaysInMonth;

// Validate year and month (ignore day of week and milliseconds) if (wYear > 9999 || wMonth < 1 || wMonth > 12) return FALSE;

// Check for leap year and set the number of days in the month bLeapYear = ((wYear & 3) == 0) && ((wYear % 100) != 0 || (wYear % 400) == 0);

nDaysInMonth = MonthDays[wMonth] - MonthDays[wMonth-1] + ((bLeapYear && wDay == 29 && wMonth == 2) ? 1 : 0);

// Finish validating the date if (wDay < 1 || wDay > nDaysInMonth || wHour > 23 || wMinute > 59 || wSecond > 59) return FALSE;

// Cache the date in days and time in fractional days //It is a valid date; make Jan 1, 1AD be 1 nDate = wYear*365L + wYear/4 - wYear/100 + wYear/400 + MonthDays[wMonth-1] + wDay;

// If leap year and it's before March, subtract 1: if (wMonth <= 2 && bLeapYear) --nDate;

// Offset so that 12/30/1899 is 0 nDate -= 693959L;

dblTime = (((long)wHour * 3600L) + // hrs in seconds              ((long)wMinute * 60L) +  // mins in seconds              ((long)wSecond)) / 86400L;

*dtDest = (double) nDate + ((nDate >= 0) ? dblTime : -dblTime); return TRUE; }

void TestConversion {  BOOL bRet; DBTIMESTAMP dbTimeStamp; DATE d;

dbTimeStamp.year = 1900; dbTimeStamp.month = 1; dbTimeStamp.day = 1; dbTimeStamp.hour = 0; dbTimeStamp.minute = 0; dbTimeStamp.second = 0;

bRet = OleDateFromTm(dbTimeStamp.year, dbTimeStamp.month,                         dbTimeStamp.day, dbTimeStamp.hour,                          dbTimeStamp.minute, dbTimeStamp.second, &d); if (bRet) //succeeded... {     //d should equal 2.00 } }

