c++时间函数


  1. //根据数值时间转换成字符串时间
  2. BOOL GetStrFromTime(time_t iTimeStamp, char *pszTime)  
  3. {  
  4.    tm *pTmp = localtime(&iTimeStamp);  
  5.    if (pTmp == NULL)  
  6.    {  
  7.       return FALSE;  
  8.    }  
  9.    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);  
  10.    return TRUE;  
  11. }  

2、精准延时

GetTickCount()

      调用函数需包含windows.h。得到的是系统运行的时间 精确到毫秒,测试程序如下:[c-shap] view pla

  1. #include <iostream>  
  2. #include <windows.h>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     double start = GetTickCount();  
  7.     Sleep(1000);  
  8.     double  end=GetTickCount();  
  9.     cout << "GetTickCount:" << end-start << endl;  
  10.         return 0;  
  11. }  

3.字符串到数值

  1. unsigned int GetTimeStampByStr( const char* pDate, int32 iNameSize )   
  2. {  
  3.    const char* pStart = pDate;  
  4.      
  5.    char szYear[5], szMonth[3], szDay[3], szHour[3], szMin[3], szSec[3];  
  6.      
  7.    szYear[0]   = *pDate++;  
  8.    szYear[1]   = *pDate++;  
  9.    szYear[2]   = *pDate++;  
  10.    szYear[3]   = *pDate++;  
  11.    szYear[4]   = 0x0;  
  12.      
  13.    ++pDate;  
  14.      
  15.    szMonth[0]  = *pDate++;  
  16.    szMonth[1]  = *pDate++;  
  17.    szMonth[2]  = 0x0;  
  18.      
  19.    ++pDate;  
  20.      
  21.    szDay[0]    = *pDate++;   
  22.    szDay[1]    = *pDate++;  
  23.    szDay[2]    = 0x0;  
  24.      
  25.    ++pDate;  
  26.      
  27.    szHour[0]   = *pDate++;  
  28.    szHour[1]   = *pDate++;  
  29.    szHour[2]   = 0x0;  
  30.      
  31.    ++pDate;  
  32.      
  33.    szMin[0]    = *pDate++;  
  34.    szMin[1]    = *pDate++;  
  35.    szMin[2]    = 0x0;  
  36.      
  37.    ++pDate;  
  38.      
  39.    szSec[0]    = *pDate++;  
  40.    szSec[1]    = *pDate++;  
  41.    szSec[2]    = 0x0;  
  42.      
  43.    tm tmObj;  
  44.      
  45.    tmObj.tm_year = atoi(szYear)-1900;  
  46.    tmObj.tm_mon  = atoi(szMonth)-1;  
  47.    tmObj.tm_mday = atoi(szDay);  
  48.    tmObj.tm_hour = atoi(szHour);  
  49.    tmObj.tm_min  = atoi(szMin);  
  50.    tmObj.tm_sec  = atoi(szSec);  
  51.    tmObj.tm_isdst= -1;  
  52.      
  53.    return mktime(&tmObj);  
  54. }  

4.获取当前时间

  1. #include <iostream>  
  2. #include <windows.h>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     SYSTEMTIME start; //windows.h中  
  7.     GetLocalTime(&start);//time.h的tm结构体一样的效果  
  8.     cout<< start.year << endl;  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值