- //根据数值时间转换成字符串时间
- BOOL GetStrFromTime(time_t iTimeStamp, char *pszTime)
- {
- tm *pTmp = localtime(&iTimeStamp);
- if (pTmp == NULL)
- {
- return FALSE;
- }
- sprintf(pszTime, "%d-%d-%d %d:%d:%d", pTmp->tm_year + 1900, pTmp->tm_mon + 1, pTmp->tm_mday, pTmp->tm_hour, pTmp->tm_min, pTmp->tm_sec);
- return TRUE;
- }
2、精准延时
GetTickCount()
调用函数需包含windows.h。得到的是系统运行的时间 精确到毫秒,测试程序如下:[c-shap] view pla
- #include <iostream>
- #include <windows.h>
- using namespace std;
- int main()
- {
- double start = GetTickCount();
- Sleep(1000);
- double end=GetTickCount();
- cout << "GetTickCount:" << end-start << endl;
- return 0;
- }
3.字符串到数值
- unsigned int GetTimeStampByStr( const char* pDate, int32 iNameSize )
- {
- const char* pStart = pDate;
- char szYear[5], szMonth[3], szDay[3], szHour[3], szMin[3], szSec[3];
- szYear[0] = *pDate++;
- szYear[1] = *pDate++;
- szYear[2] = *pDate++;
- szYear[3] = *pDate++;
- szYear[4] = 0x0;
- ++pDate;
- szMonth[0] = *pDate++;
- szMonth[1] = *pDate++;
- szMonth[2] = 0x0;
- ++pDate;
- szDay[0] = *pDate++;
- szDay[1] = *pDate++;
- szDay[2] = 0x0;
- ++pDate;
- szHour[0] = *pDate++;
- szHour[1] = *pDate++;
- szHour[2] = 0x0;
- ++pDate;
- szMin[0] = *pDate++;
- szMin[1] = *pDate++;
- szMin[2] = 0x0;
- ++pDate;
- szSec[0] = *pDate++;
- szSec[1] = *pDate++;
- szSec[2] = 0x0;
- tm tmObj;
- tmObj.tm_year = atoi(szYear)-1900;
- tmObj.tm_mon = atoi(szMonth)-1;
- tmObj.tm_mday = atoi(szDay);
- tmObj.tm_hour = atoi(szHour);
- tmObj.tm_min = atoi(szMin);
- tmObj.tm_sec = atoi(szSec);
- tmObj.tm_isdst= -1;
- return mktime(&tmObj);
- }
4.获取当前时间
- #include <iostream>
- #include <windows.h>
- using namespace std;
- int main()
- {
- SYSTEMTIME start; //windows.h中
- GetLocalTime(&start);//time.h的tm结构体一样的效果
- cout<< start.year << endl;
- }