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

<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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值