C++北京时间(字符串)和时间戳相互转换
/*********************************************************************
Describe : 将北京时间转换成时间戳
Input : pStr 字符串时间
Output : tempTime 整形时间
*********************************************************************/
bool TranTime(const char *pStr, time_t &tempTime);
/*********************************************************************
Describe : 将时间戳转换成北京时间
Input : tempTime 整形时间
Output : pStr 字符串时间
*********************************************************************/
bool TranUtcToStrTime(time_t tempTime,char *pStr);
bool TranStrTimeToTimestamp(const char *pStr, time_t &tampTime)
{
tm m;
if(sscanf(pStr, "%04d-%02d-%02dT%02d:%02d:%02d",&m.tm_year, &m.tm_mon, &m.tm_mday, &m.tm_hour, &m.tm_min, &m.tm_sec) != 6)
{
printf("sscanf failed, str:%s\n", pStr);
return false;
}
m.tm_year -=1900;
m.tm_mon = m.tm_mon - 1;
tampTime = mktime(&m);
return true;
}
bool TranTimestampToStrTime(time_t tampTime, char *pStr)
{
tm* stLocal = localtime(&tampTime);
strftime(pStr, 64, "%Y-%m-%d %H:%M:%S", stLocal);
return true;
}