home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c09 / lab01 / ex01 / employeeddx.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  2.5 KB  |  99 lines

  1. #ifndef _EMPLOYEE_DDX_CPP
  2. #define _EMPLOYEE_DDX_CPP
  3.  
  4.  
  5. #include <stdio.h>
  6.  
  7.  
  8. //Add a data exchange for the date field
  9. //See Tech Note 026,and articles Q114961 and Q110719 for more information
  10. //This is a global function
  11. void DDX_FieldText(
  12.         CDataExchange * pDX, UINT nID,
  13.         TIMESTAMP_STRUCT & date, CRecordset * pSet )
  14. {
  15.     //Saving, we need to convert from string to date
  16.  
  17.     if ( pDX->m_bSaveAndValidate )
  18.     {
  19.         char caDate[120];
  20.         pDX->PrepareEditCtrl( nID );
  21.         pDX->m_pDlgWnd->GetDlgItem( nID )->
  22.             GetWindowText( caDate, sizeof caDate );
  23.         int nMonth, nDay, nYear, nFields;
  24.         nMonth = nDay = nYear = 0;
  25.         nFields = sscanf( caDate, "%d/%d/%d",
  26.                 & nMonth, & nDay, & nYear );
  27.         if ( 3 == nFields )    //Did we get three values?
  28.         {
  29.             if ( nYear < 100 )
  30.                 nYear     += 1900;
  31.             date.year      = nYear;
  32.             date.month      = nMonth;
  33.             date.day      = nDay;
  34.             date.hour      = 0;
  35.             date.minute      = 0;
  36.             date.second      = 0;
  37.             date.fraction = 0;
  38.             return;
  39.         }
  40.         AfxMessageBox( "Use date format:\nm/d/yyyy",
  41.             MB_OK | MB_ICONEXCLAMATION );
  42.         pDX->Fail( );    //Put focus back to field
  43.     }
  44.     else
  45.     {
  46.         CString strDate;
  47.         if ( 12 < date.month &&  31 < date.day )    //Uninitialized date
  48.             strDate = "";
  49.         else
  50.             strDate.Format( "%u/%u/%u",
  51.                 date.month, date.day, date.year );
  52.         pDX->m_pDlgWnd->GetDlgItem( nID )->
  53.             SetWindowText( strDate );
  54.     }
  55. }
  56.  
  57. void DDV_Date( CDataExchange * pDX, TIMESTAMP_STRUCT & date, CRecordset * 
  58. pSet  )
  59. {
  60.     if ( ! pDX->m_bSaveAndValidate )
  61.         return;
  62.     CString str1, str2, msg;
  63.     int nMaxDays = 30;    //Usually they all have 30 days except Feb
  64.     int month = date.month;
  65.     
  66.     nMaxDays += month < 8 ? //Odd months are longer until July
  67.         month & 1 : ! ( month & 1 );
  68.     if ( 2 == month )    //February
  69.     {
  70.         int nLeapYear =
  71.             0 == date.year % 4;            //Leap years always evenly divisible by 4
  72.         if ( nLeapYear )
  73.             nLeapYear =
  74.                 0 == date.year % 100    //If the year is even div by 100
  75.                 ?
  76.                 0 == date.year % 400    //Then it must also be even div by 400
  77.                 :
  78.                 1;
  79.  
  80.         nMaxDays = 28 + nLeapYear;    //Add a day for leap years
  81.     }
  82.     if ( month < 1 || month > 12 )
  83.     {
  84.         str1 = "Month must be between 1 and 12\n";
  85.         nMaxDays = 31;    //Not more than 31 days in an invalid month
  86.     }
  87.     if ( date.day < 1 || date.day > nMaxDays )
  88.         str2.Format( "\nDay must be between 1 and %d", nMaxDays );
  89.     msg = str1 + str2;
  90.     if ( msg.IsEmpty( ) )
  91.         return;        //No problems noted
  92.     AfxMessageBox(msg, MB_OK | MB_ICONEXCLAMATION );
  93.     pDX->Fail( );
  94. }
  95.  
  96.  
  97.  
  98.  
  99. #endif    //Already included