⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dateutil.cpp

📁 API经典入门
💻 CPP
字号:
// This file contains the implementations for utilities 
// relating to CDate.
// Filename : DateUtil.cpp

#include "stdafx.h"
#include "DateUtil.h"

#define RFX_DATE_TYPE 15

void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, CDate& value)
{
	HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC);
	CString tempS;
	
	if (pDX->m_bSaveAndValidate)
	{
		int nLen = ::GetWindowTextLength(hWndCtrl);
		::GetWindowText(hWndCtrl, tempS.GetBufferSetLength(nLen), nLen+1);

		if (nLen == 0)
			value = CDate(); // Null date
		else if (!value.StringToDate(tempS)) // convert string to date
		{
			AfxMessageBox("Invalid Date!");
			tempS.Empty();
			// Fail will throw an exception whose alternate
			// flow of control will bypass the normal end-scope
			// clean-up.  Therefore we must manually destroy
			// dynamic objects like CString.
			pDX->Fail();
		}
	}
	else
	{
		::SetWindowText(hWndCtrl, value.DateString());
	}
}

// if the ODBC classes are available then allow the field exchange
// mechanisms to be compiled
#ifdef __AFXDB_H__
void AFXAPI DDX_FieldText(CDataExchange* pDX, int nIDC, CDate& value,
	CRecordset* pRecordset)
{
	ASSERT_VALID(pRecordset);
	HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC);
	CString tempS;
	//CDate tempDate;
	
	if (pDX->m_bSaveAndValidate)
	{
		int nLen = ::GetWindowTextLength(hWndCtrl);
		::GetWindowText(hWndCtrl, tempS.GetBufferSetLength(nLen), nLen+1);

		if (nLen == 0)
		{
			if (pRecordset->IsFieldNullable(&value))
				pRecordset->SetFieldNull(&value, TRUE); // Null DB field
		}
		//else if (!tempDate.StringToDate(tempS)) // convert string to date
		else if (!value.StringToDate(tempS)) // convert string to date
		{
			AfxMessageBox("Invalid Date!");
			tempS.Empty();
			// Fail will throw an exception whose alternate
			// flow of control will bypass the normal end-scope
			// clean-up.  Therefore we must manually destroy
			// dynamic objects like CString.
			pDX->Fail();
		}
		/*else
		{
			value.year = tempDate.Year();
			value.month = tempDate.Month();
			value.day = tempDate.Day();
			
		}*/
	}
	else
	{
		//tempDate = CDate(value.year, value.month, value.day);
		//::SetWindowText(hWndCtrl, tempDate.DateString("%m/%d/%Y"));
		::SetWindowText(hWndCtrl, value.DateString("%m/%d/%Y"));
	}
}


///////////////////////////////////////////////////////////
// Implementation of the RFX_Date function

// DATE_STRUCT is a portable SQL type for C
// it's defined in sqlext.h		

void AFXAPI RFX_Date(CFieldExchange* pFX, const char *szName,
	CDate& value)
{
	ASSERT(AfxIsValidAddress(pFX, sizeof(CFieldExchange)));
	ASSERT(AfxIsValidString(szName));

	CDate NULL_DATE_OBJ;
	
	// extract the DATE_STRUCT out of CDate
	DATE_STRUCT* pds_value = (DATE_STRUCT*)( ((int)&value) + 4 );
	void* pvoid = (void*)&value;
	if (pvoid == pFX->m_pvField)
		pFX->m_pvField = (void*)pds_value;
		
	//if (pFX->m_pvField == (void*)&value)
		//pFX->m_pvField = (void*)pds_value;
	
	UINT nField;
	if (!pFX->IsFieldType(&nField))
		return;

	LONG* plLength = pFX->m_prs->GetFieldLength(pFX);
	switch (pFX->m_nOperation)
	{
		case CFieldExchange::BindFieldToColumn:
			if (pFX->GetColumnType(nField) != SQL_C_DATE)
				pFX->m_prs->ThrowDBException(AFX_SQL_ERROR_FIELD_SCHEMA_MISMATCH);
			// fall through

		default:
LDefault:
			pFX->Default(szName, pds_value, plLength, SQL_C_DATE,
				sizeof(*pds_value), 6);
			return;

		case CFieldExchange::Fixup:
			if (*plLength == SQL_NULL_DATA)
			{
				pFX->m_prs->SetFieldFlags(nField,
					AFX_SQL_FIELD_FLAG_NULL, pFX->m_nFieldType);
				value = NULL_DATE_OBJ;
			}
			return;

		case CFieldExchange::SetFieldNull:
			if ((pFX->m_pvField == NULL &&
				pFX->m_nFieldType == CFieldExchange::outputColumn) ||
				pFX->m_pvField == pds_value)
			{
				if (pFX->m_bField)
				{
					// Mark fields null
					pFX->m_prs->SetFieldFlags(nField,
						AFX_SQL_FIELD_FLAG_NULL, pFX->m_nFieldType);
					value = NULL_DATE_OBJ;
					*plLength = SQL_NULL_DATA;
				}
				else
				{
					pFX->m_prs->ClearFieldFlags(nField,
						AFX_SQL_FIELD_FLAG_NULL, pFX->m_nFieldType);
					*plLength = sizeof(*pds_value);
				}
#ifdef _DEBUG
				pFX->m_bFieldFound = TRUE;
#endif // _DEBUG
			}
			return;

		case CFieldExchange::MarkForAddNew:
			// can force writing of psuedo-null value (as a non-null) by setting field dirty
			if (!pFX->m_prs->IsFieldFlagDirty(nField, pFX->m_nFieldType))
			{
				if ( value != NULL_DATE_OBJ )
				{
					pFX->m_prs->SetFieldFlags(nField,
						AFX_SQL_FIELD_FLAG_DIRTY, pFX->m_nFieldType);
					pFX->m_prs->ClearFieldFlags(nField,
						AFX_SQL_FIELD_FLAG_NULL, pFX->m_nFieldType);
				}
			}
			return;

		case CFieldExchange::MarkForUpdate:
		case CFieldExchange::StoreField:
			if ( value == NULL_DATE_OBJ ) 
				pFX->m_prs->SetFieldFlags(nField,
					AFX_SQL_FIELD_FLAG_NULL, pFX->m_nFieldType);
			else
				pFX->m_prs->ClearFieldFlags(nField,
					AFX_SQL_FIELD_FLAG_NULL, pFX->m_nFieldType);
			goto LDefault;

		case CFieldExchange::GetFieldInfoValue:
			if (pFX->m_pfi->pv == pds_value)
			{
				pFX->m_pfi->nField = nField-1;
				goto LFieldFound;
			}
			return;

		case CFieldExchange::GetFieldInfoOrdinal:
			if (nField-1 == pFX->m_pfi->nField)
			{
LFieldFound:
				pFX->m_pfi->nDataType = RFX_DATE_TYPE;
				pFX->m_pfi->strName = szName;
				pFX->m_pfi->pv = pds_value;
				pFX->m_pfi->dwSize = sizeof(*pds_value);
				// Make sure field found only once
				ASSERT(pFX->m_bFieldFound == FALSE);
				pFX->m_bFieldFound = TRUE;
			}
			return;
		}
}

#endif


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -