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

📄 datetime.cpp

📁 ◆◆◆ 《投掷飞镖记分工具》◆◆◆ 操作系统 : Windows Mobile 5.0 说明 : 中秋节和家人赏月
💻 CPP
字号:
#include "StdAfx.h"
#include "PublicFunc.h"

const TCHAR *f_WeekStr[] = 
{
	_T(""), _T("Sun."), _T("Mon."), _T("Tue."), _T("Wed."), _T("Thu."), _T("Fri."), _T("Sta."), _T(""),
};

const TCHAR *f_PAM[] = 
{
	_T("A.M."), _T("P.M."), _T(""),
};

time_t GetCurrentTime ()
{
	SYSTEMTIME stm;
	GetLocalTime ( &stm );
	CTime tNow ( stm );
	return (time_t)tNow.GetTime ();
}

/********************************************************************************
* Function Type	:	public
* Parameter		:	buf		-	输出缓冲
* Return Value	:	字符个数
* Description	:	获取当前时间的字符串,如:2003-10-01 12:00:00
*********************************************************************************/
DWORD GetCurTimeString ( TCHAR *buf, time_t tNow/*=0*/ )
{
	ASSERT_ADDRESS ( buf, DATETIME_TYPE_LENGTH );
	CTime cTime;
	if ( tNow == 0 )
	{
		cTime = CTime::GetCurrentTime();
	}
	else
	{
		cTime = tNow;
	}
	CString csNow = cTime.Format ( _T("%Y-%m-%d %H:%M:%S") );
	return (DWORD)hwSnprintf ( buf, DATETIME_TYPE_LENGTH, _T("%s"), csNow );
}

CString GetCurTimeString ( time_t tNow/*=0*/ )
{
	TCHAR szTime[DATETIME_TYPE_LENGTH+1] = {0};
	GetCurTimeString ( szTime, tNow );
	return szTime;
}

/********************************************************************************
* Function Type	:	Global
* Parameter		:	t			-	待处理的时间(秒)
*					buf			-	表示时间的字符串
*					Flag		-	标志
*									0	-	"2001-08-09 18:03:30"
*									1	-	"200108"
*									2	-	"0108"
*									3	-	"20010809"
*									4	-	"Wed. P.M. 06:03" 翻译成中文:"星期三 下午 06:03"
*									5	-	"18:03"
*									6	-	"2001-08-09"
*									7	-	"08/09"
*									8	-	"08/09(Wed.) P.M. 06:03" 翻译成中文:"08/09(星期三) 下午 06:03"
*									9	-	"08-09 18:03"
*									10	-	"2001-08"
*									11	-	"09180330"
*									12	-	"2001-08-09 18_03_30"
* Return Value	:	return the string length.
* Description	:	将一个time_t格式的时间值转换成相应的字符串(按年、月、日、时、
*					分、秒的顺序,年用四个字符,其他项用两个字符表示)
*********************************************************************************/
int ConvertTimeToStr(time_t t, TCHAR *buf, int nSize, int Flag /* = 0 */)
{
	ASSERT_ADDRESS ( buf, DATETIME_TYPE_LENGTH );

	if(t < 1)
	{
		lstrcpyn ( buf, _T("1970-01-01 00:00:00"), nSize );
		ASSERT ( FALSE );
		return 0;
	}
	CTime cTime ( t );
	switch ( Flag )
	{
	case 0:
	default:
		return hwSnprintf( (LPTSTR)buf, nSize, _T("%s"), cTime.Format ( _T("%Y-%m-%d %H:%M:%S") ) );
	case 1:
		return hwSnprintf((LPTSTR)buf, nSize, _T("%s"), cTime.Format ( _T("%Y%m") ) );
	case 2:
		return hwSnprintf((LPTSTR)buf, nSize, _T("%s"), cTime.Format ( _T("%y-%m") ) );
	case 3:
		return hwSnprintf((LPTSTR)buf, nSize, _T("%s"), cTime.Format ( _T("%Y%m%d") ) );
	case 4:
		{
			return hwSnprintf((LPTSTR)buf, nSize, _T("{%s}{ }{%s}{ }%.2d:%.2d"), 
				f_WeekStr [ cTime.GetDayOfWeek() ], f_PAM[(cTime.GetHour()>=12)?1:0], cTime.GetHour()%12, cTime.GetMinute() );
		}
	case 5:
		return hwSnprintf((LPTSTR)buf, nSize, _T("%s"), cTime.Format ( _T("%H:%M") ) );
	case 6:
		return hwSnprintf((LPTSTR)buf, nSize, _T("%s"), cTime.Format ( _T("%Y-%m-%d") ) );
	case 7:
		return hwSnprintf((LPTSTR)buf, nSize, _T("%s"), cTime.Format ( _T("%m/%d") ) );
	case 8:
		{
			return hwSnprintf((LPTSTR)buf, nSize, _T("%.2d/%.2d({%s}){ }{%s}{ }%.2d:%.2d"), 
				cTime.GetMonth(), cTime.GetDay(), f_WeekStr [ cTime.GetDayOfWeek() ],
				f_PAM[(cTime.GetHour()>=12)?1:0],
				cTime.GetHour()%12, cTime.GetMinute() );
		}
	case 9:
		return hwSnprintf((LPTSTR)buf, nSize, _T("%s"), cTime.Format ( _T("%m-%d %H:%M") ) );
	case 10:
		return hwSnprintf((LPTSTR)buf, nSize, _T("%s"), cTime.Format ( _T("%Y-%m") ) );
	case 11:
		return hwSnprintf((LPTSTR)buf, nSize, _T("%s"), cTime.Format ( _T("%d%H%M%S") ) );
	case 12:
		return hwSnprintf((LPTSTR)buf, nSize, _T("%s"), cTime.Format ( _T("%Y-%m-%d %H_%M_%S") ) );
	}

	return 0;
}

CString ConvertTimeToStr(time_t t, int Flag /*=0*/)
{
	TCHAR buf[256] = {0};
	ConvertTimeToStr ( t, buf, sizeof(buf), Flag );
	return buf;
}

//
// 将表示时长的“秒”转为时间型字符串,如:80 秒转后为“00:01:20”
//
TCHAR *ConvertTimeLongToStr ( int nTimeLong, TCHAR *szTimeLong, int nSize )
{
	if ( nTimeLong == 0 ) hwSnprintf ( szTimeLong, nSize, _T("00") );
	int nData[8] = {0}, count = 0;
	nData[count++] = ( (UINT)nTimeLong / (3600*24) );							// 天
	nData[count++] = ( (UINT)nTimeLong % (3600*24) ) / 3600;					// 时
	nData[count++] = ( ( (UINT)nTimeLong % (3600*24) ) % 3600 ) / 60;			// 分
	nData[count++] = ( ( ( (UINT)nTimeLong % (3600*24) ) % 3600 ) % 60 ) / 1;	// 秒

	count = 0;
	for ( int i=0; i<4 && count < nSize; i++ )
	{
		if ( nData[i] > 0 || count > 0 )
		{
			if ( count > 0 )
			{
				count += hwSnprintf ( szTimeLong+count, nSize-count-1, _T("%c"), (i==1)?' ':':' );
			}
			count += hwSnprintf ( szTimeLong+count, nSize-count-1, _T("%02d"), nData[i] );
		}
	}
	return szTimeLong;
}

CString ConvertTimeLongToStr ( int nTimeLong )
{
	TCHAR szTimeLong[255] = {0};
	CString csTimeLong = ConvertTimeLongToStr ( nTimeLong,szTimeLong,sizeof(szTimeLong) );

	return csTimeLong;
}

#define  TIME_START_YEAR  1900
BOOL CopyBuffer_Date ( int *data, SYSTEMTIME &SysTime )
{
	const int nMinDateDigitCount = 3;
	ASSERT_ADDRESS ( data, nMinDateDigitCount * sizeof(int) );

	/********年(1000 ~ 9999)********/
	if ( data[0] < 1000 || data[0] >= 9999 )
		return FALSE;
	SysTime.wYear = data[0];

	/********月(1--12)********/
	if ( data[1] < 1 || data[1] > 12 )
		return FALSE;
	SysTime.wMonth = data[1];

	/********日(1--31)********/
	if ( data[2] < 1 && data[2] > 31 )
		return FALSE;
	SysTime.wDay = data[2];

	return TRUE;
}
BOOL CopyBuffer_Time ( int *data, SYSTEMTIME &SysTime )
{
	const int nMinDateDigitCount = 3;
	ASSERT_ADDRESS ( data, nMinDateDigitCount * sizeof(int) );

	/********时(0--23)********/
	if ( data[0] <0 || data[0] > 23 )
		return FALSE;
	SysTime.wHour = data[0];

	/********分(0--59)********/
	if ( data[1] < 0 || data[1] > 59 )
		return FALSE;
	SysTime.wMinute = data[1];

	/********秒********/
	if ( data[2] < 0 || data[2] > 59 )
		return FALSE;
	SysTime.wSecond = data[2];

	return TRUE;
}

//
// ConvertStrToCTime() 将一个表示日期的字符串(按年、月、日、时、分、秒的顺序,
// 如"2001-08-09 18:03:30")转成 CTime 格式.
// return : ------------------------------------------------------------
//		0	-	错误
//		1	-	是时期时间
//		2	-	是日期
//		3	-	是时间
//
int ConvertStrToCTime(TCHAR *chtime, CTime &cTime )
{
	int  i, j, k;
	TCHAR tmpbuf[8] = {0};
	int  value[6] = {0};
	SYSTEMTIME SysTime = {0};

	if ((!chtime) ) return FALSE; /* invalid parameter */

	memset((void *)value, 0, sizeof(value));
	for (i=0, j=0, k=0;  ; i++)
	{
		if (chtime[i]<_T('0') || chtime[i]>_T('9')) /* 非数字字符 */
		{
			tmpbuf[j] = _T('\0');
			if ( j > 0 )
			{
				value[k++] = atoi ( GetMultiByteChar ( tmpbuf ) );
				j = 0;
				if ( k >= 6 ) break;
			}
			if ( chtime[i] == _T('\0') ) break;
		}
		else if (j < 7) tmpbuf[j++] = chtime[i];
	}
	if ( k < 3 ) return 0;
	if	(
			CalcCharCount ( chtime, _T('-') ) < 2
			&&
			CalcCharCount ( chtime, _T('/') ) < 2
			&&
			CalcCharCount ( chtime, _T(':') ) < 2
		)
		return 0;

	int nRet = 0;
	// 是日期时间
	if	(
			( k>=6 )
			&&
			CopyBuffer_Date ( value, SysTime )
			&&
			CopyBuffer_Time ( &value[3], SysTime )
		)
	{
		nRet = 1;
	}
	// 是日期
	else if	(
			(k>=3)
			&&
			CopyBuffer_Date ( value, SysTime )
		)
	{
		nRet = 2;
	}

	// 是时间
	if	(
			(k>=3)
			&&
			CopyBuffer_Time ( value, SysTime )
		)
	{
		nRet = 3;
	}
	
	if ( SysTime.wYear < 1971 )
		SysTime.wYear = 1971;
	if ( SysTime.wMonth < 1 )
		SysTime.wMonth = 1;
	if ( SysTime.wDay < 1 )
		SysTime.wDay = 1;

	CTime tTemp ( SysTime );
	cTime = tTemp;

	return nRet;
}

time_t ConvertStrToTime(TCHAR *chtime)
{
	CTime cTime;

	if (!chtime) return 0;
	if ( ConvertStrToCTime(chtime, cTime) == 0 )
		return 0;
	return (time_t)cTime.GetTime();
}

⌨️ 快捷键说明

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