home *** CD-ROM | disk | FTP | other *** search
- // mdy.cpp : Implementation of Cmdy
- #include "stdafx.h"
- #include "MoDaYrX.h"
- #include "mdy.h"
-
- #include <stdio.h> // for sscanf
-
- /////////////////////////////////////////////////////////////////////////////
- // Cmdy
-
- STDMETHODIMP Cmdy::MDYfromBSTR(BSTR bstrDate, short * pnM, short * pnD, short * pnY)
- {
- short nMonth, nDay, nYear, nFields;
-
- nMonth = nDay = nYear = 0;
- _bstr_t bstr(bstrDate);
- nFields = sscanf( ( char * ) bstr, "%hd/%hd/%hd",
- & nMonth, & nDay, & nYear );
- if ( 3 == nFields ) //Did we get three values?
- {
- if ( nYear < 100 )
- nYear += 1900;
- * pnY = nYear;
- * pnM = nMonth;
- * pnD = nDay;
- return S_OK;
- }
- return S_FALSE;
- }
-
- STDMETHODIMP Cmdy::ValidateMDY(BSTR * pbstrMessage, short nM, short nD, short nY)
- {
- char caBuff[80];
- _bstr_t bstrMsg1, bstrMsg2;
- short nMaxDays = 30; //Usually they all have 30 days except Feb
- short nMonth = nM;
-
- nMaxDays += nMonth < 8 ? //Odd months are longer until July
- nMonth & 1 : ! ( nMonth & 1 );
- if ( 2 == nMonth ) //February
- {
- short nLeapYear =
- 0 == nY % 4; //Leap years always evenly divisible by 4
- if ( nLeapYear )
- nLeapYear =
- 0 == nY % 100 //If the year is even div by 100
- ?
- 0 == nY % 400 //Then it must also be even div by 400
- :
- 1;
-
- nMaxDays = 28 + nLeapYear; //Add a day for leap years
- }
- if ( nMonth < 1 || nMonth > 12 )
- {
- bstrMsg1 = "Month must be between 1 and 12\n";
- nMaxDays = 31; //Not more than 31 days in an invalid month
- }
- if ( nD < 1 || nD > nMaxDays )
- {
- wsprintf( caBuff, "\nDay must be between 1 and %hd", nMaxDays );
- bstrMsg2 = caBuff;
- }
-
- if ( ! bstrMsg1 && ! bstrMsg2 ) //If neither contains a message
- return S_OK; //No problems noted
-
- _bstr_t msg( bstrMsg1 ); //String with the first message (or NULL)
- msg += bstrMsg2; //Concatenate second message (or NULL)
- * pbstrMessage = msg; //Modify parameter to contain message
-
- return S_FALSE; //Something was wrong
- }
-