KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q241718: HOWTO: Convert a DBTIMESTAMP into a DATE

Article: Q241718
Product(s): Microsoft C Compiler
Version(s): winnt:4.0,4.2,5.0,6.0
Operating System(s): 
Keyword(s): kbnokeyword kbAutomation kbMFC kbVC400 kbVC420 kbVC500 kbVC600 kbDSupport kbGrpDSMFCATL
Last Modified: 07-MAY-2001

-------------------------------------------------------------------------------
The information in this article applies to:

- Microsoft Visual C++, version 4.0 
- Microsoft Visual C++, 32-bit Enterprise Edition, versions 4.2, 5.0, 6.0 
- Microsoft Visual C++, 32-bit Professional Edition, versions 4.2, 5.0, 6.0 
- Microsoft Visual C++, 32-bit Learning Edition, version 6.0 
-------------------------------------------------------------------------------

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.

MORE INFORMATION
================

If You Are Using MFC, Use this Code
-----------------------------------

  #include <oledb.h>

  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 Using MFC, Use this Code
-----------------------------------

  //headers needed
  #include <windows.h>
  #include <oledb.h>

  // 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
     }
  }

REFERENCES
==========

COleDateTime in MSDN

Additional query words: COleDateTime DBTIMESTAMP OLEDB Conversion VarDateFromStr

======================================================================
Keywords          : kbnokeyword kbAutomation kbMFC kbVC400 kbVC420 kbVC500 kbVC600 kbDSupport kbGrpDSMFCATL 
Technology        : kbVCsearch kbVC400 kbAudDeveloper kbVC420 kbVC500 kbVC600 kbVC32bitSearch kbVC500Search
Version           : winnt:4.0,4.2,5.0,6.0
Issue type        : kbhowto

=============================================================================

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.