datetime.cpp

来自「GPS数据采集程序(通过串口读取GPS数据」· C++ 代码 · 共 80 行

CPP
80
字号
// datetime.cpp : DDX_FieldDateTime的执行代码 

#include "stdafx.h" 
#include "datetime.h" 

void AFXAPI DDX_FieldDateTime(CDataExchange* pDX, int nIDC, CTime ts, CRecordset* pSet ) 
{ 
	//下面Edit控件的HWND是为DDX 或DDV作准备的 
	HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC); 
	if(pDX->m_bSaveAndValidate)
	{
		if (!GetDateTime(hWndCtrl, ts))
		{ 
			//不能得到日期和时间; 
			AfxMessageBox("无效时间"); 
			pDX->Fail();
		}
	}
	else 
	{
		SetDateTime(hWndCtrl, ts);
	}
}

BOOL GetDateTime(CWnd* pWnd, CTime ts)
{ 
	ASSERT(pWnd != NULL); 
	return GetDateTime(pWnd->m_hWnd, ts); 
} 

//从Edit box中取得日期和时间值
BOOL GetDateTime(HWND hWnd, CTime ts) 
{
	const int DATETIME_SIZE = 64; 
	char sBuffer[DATETIME_SIZE+1]; 
	CString strBuffer; 
	COleDateTime datetime; 
	
	::GetWindowText(hWnd, sBuffer, DATETIME_SIZE); 
	
	//从Edit box取值 
	strBuffer = sBuffer; 
	if(strBuffer.IsEmpty())
	{
		ts.GetCurrentTime(); 
		return TRUE;
	} 
	// 注意ParseDateTime是帮助你测试日期和时间和时间完整性的
	datetime.ParseDateTime(strBuffer, LOCALE_NOUSEROVERRIDE, LANG_USER_DEFAULT );
	if( datetime.GetStatus() == COleDateTime::valid) 
	{
		CTime tyj2( datetime.GetYear(),datetime.GetMonth(), datetime.GetDay(),datetime.GetHour(), datetime.GetMinute(), datetime.GetSecond());
		//再传回来
		ts=tyj2;
		return TRUE;
	}
	else
		return FALSE;
} 

void SetDateTime(CWnd* pWnd, CTime ts) 
{
	ASSERT(pWnd != NULL);
	SetDateTime(pWnd->m_hWnd, ts);
} 

//用于显示日期和时间值到Edit box
void SetDateTime(HWND hWnd, CTime ts) 
{
	COleDateTime datetime( ts.GetYear(), ts.GetMonth(), ts.GetDay(), ts.GetHour(), ts.GetMinute(), ts.GetSecond());
	CString sBuffer; 
	sBuffer = datetime.Format("%c"); 
	
	//如果日期和时间值无效的话为空字符 
	if( datetime.GetStatus() == COleDateTime::invalid ) 
		sBuffer = "";

	::SetWindowText(hWnd, sBuffer.GetBufferSetLength(sBuffer.GetLength()));
}

⌨️ 快捷键说明

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