1. 程式人生 > >日期格式和時間戳相互轉換 外帶一個獲取當前程式根目錄

日期格式和時間戳相互轉換 外帶一個獲取當前程式根目錄

/*
 *	函式:
 *		 CTimeConvertCString(時間戳轉字串日期格式)
 *  引數:
 *		tm:時間戳
 *  返回值:
 *		返回對應的日期格式字串
 * ssdwujianhua 2017/06/06 
 */
CString CUtil::CTimeConvertCString(CTime tm)
{
	CString strDate;
	strDate.Format("%04d-%02d-%02d %02d:%02d:%02d", 
		tm.GetYear(), tm.GetMonth(), tm.GetDay(),
		tm.GetHour(), tm.GetMinute(), tm.GetSecond());
	return strDate;
}

/*
 *	函式:
 *		 CStringConvertCTime(字串日期格式轉時間戳)
 *  引數:
 *		strDate:日期
 *  返回值:
 *		返回對應日期的時間戳
 * ssdwujianhua 2017/06/06 
 */
CTime CUtil::CStringConvertCTime(CString strDate)
{
	COleDateTime datetime;
	CTime tm;
	if ( datetime.ParseDateTime(strDate.GetBuffer(0)))
	{
		SYSTEMTIME systime;
		VariantTimeToSystemTime(datetime.m_dt, &systime);
		tm = systime;
	}
	return tm;   
}

/*
 *	函式:
 *		GetCurrentAppPath(獲取當前app檔案路徑) 
 *  引數:
 *		無
 *  返回值:
 *		返回當前app檔案路徑(也是當前可執行程式的根目錄)
 * ssdwujianhua 2017/06/06 
 */
CString CUtil::GetCurrentAppPath()
{
	static  BOOL bFlag = FALSE;
	static CString strPath;
	if (  !bFlag )
	{ 
		GetModuleFileName(NULL, strPath.GetBufferSetLength(MAX_PATH + 1), MAX_PATH);  
		strPath.ReleaseBuffer();  
		int nPos;  
		nPos = strPath.ReverseFind('\\');  
		strPath = strPath.Left(nPos);  
		bFlag = TRUE;
	}
	return strPath;
}