C++时间转字符串与时间差

来源:互联网 发布:蒙特卡洛算法求圆周率 编辑:程序博客网 时间:2024/06/09 03:10
<pre name="code" class="cpp">std::string getStrFromTime(time_t timet)<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: 'Heiti SC Light'; color: rgb(0, 132, 0);"><span style="font-family: Menlo;">//</span>时间转换</p>{    struct tm * pTime = localtime(&timet);    char tstr[30];    sprintf(tstr, "%d-%02d-%02d %02d:%02d:%02d",pTime->tm_year+1900,pTime->tm_mon+1,pTime->tm_mday,pTime->tm_hour,pTime->tm_min,pTime->tm_sec);    return tstr;}


unsigned int getTimeTByStr(const char *pDate){    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] = '\0';        ++pDate;        szMonth[0] = *pDate++;    szMonth[1] = *pDate++;    szMonth[2] = '\0';        ++pDate;        szDay[0] = *pDate++;    szDay[1] = *pDate++;    szDay[2] = '\0';        ++pDate;        szHour[0] = *pDate++;    szHour[1] = *pDate++;    szHour[2] = '\0';        ++pDate;        szMin[0] = *pDate++;    szMin[1] = *pDate++;    szMin[2] = '\0';        ++pDate;        szSec[0] = *pDate++;    szSec[1] = *pDate++;    szSec[2] = '\0';        struct 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;        pDate = pStart;        return mktime(&tmObj);}time_t getCurrentTimeT(){<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: 'Heiti SC Light'; color: rgb(0, 132, 0);"><span style="font-family: Menlo;">//</span>获取当前时间</p>    time_t rawtime;    time(&rawtime);    return rawtime;}int getTimeDistance(time_t frontTime, time_t lastTime){<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: 'Heiti SC Light'; color: rgb(0, 132, 0);"><span style="font-family: Menlo;">//</span>获取时间间隔</p>    int dis = difftime(lastTime, frontTime);    return dis;}


0 0
原创粉丝点击