获取系统字符串时间
std::string cuurTime()
{
char tmp[64];
time_t ptime;
time(&ptime);
strftime(tmp,sizeof(tmp),"%Y-%m-%d %H:%M:%S",localtime(&ptime));
return tmp;//获得字符串
}
std::string cuurTime()
{
char timeStr[64];
time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
struct tm * ptminfo = localtime(&tt);
strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", ptminfo);
return tmp;
}
将字符串时间转换成秒数:
time_t strToSecond(std::string time)// 20200204132000 年 月 日 时 分 秒 转 换 成 秒
{
struct tm timeTmp;
time_t seconds;
int sz = time.size();
if (sz == 8)
{
timeTmp.tm_year = atoi(time.substr(0, 4).c_str()) - 1900; //从1900 开始算起
timeTmp.tm_mon = atoi(time.substr(4, 2).c_str());
timeTmp.tm_mday = atoi(time.substr(6, 2).c_str());
timeTmp.tm_hour = 0;
timeTmp.tm_min = 0;
timeTmp.tm_sec = 0;
}
else if (sz == 10)
{
timeTmp.tm_year = atoi(time.substr(0, 4).c_str()) - 1900; //从1900 开始算起
timeTmp.tm_mon = atoi(time.substr(4, 2).c_str());
timeTmp.tm_mday = atoi(time.substr(6, 2).c_str());
timeTmp.tm_hour = atoi(time.substr(8, 2).c_str());
timeTmp.tm_min = 0;
timeTmp.tm_sec =0;
}
else if (sz==12)
{
timeTmp.tm_year = atoi(time.substr(0, 4).c_str()) - 1900; //从1900 开始算起
timeTmp.tm_mon = atoi(time.substr(4, 2).c_str());
timeTmp.tm_mday = atoi(time.substr(6, 2).c_str());
timeTmp.tm_hour = atoi(time.substr(8, 2).c_str());
timeTmp.tm_min = atoi(time.substr(10, 2).c_str());
timeTmp.tm_sec = 0;
}
else if (sz == 14)
{
timeTmp.tm_year = atoi(time.substr(0, 4).c_str()) - 1900; //从1900 开始算起
timeTmp.tm_mon = atoi(time.substr(4, 2).c_str());
timeTmp.tm_mday = atoi(time.substr(6, 2).c_str());
timeTmp.tm_hour = atoi(time.substr(8, 2).c_str());
timeTmp.tm_min = atoi(time.substr(10, 2).c_str());
timeTmp.tm_sec = atoi(time.substr(12, 2).c_str());
}
else
{
std::cout << "输入错误!" << std::endl;
}
seconds = mktime(&timeTmp);
return seconds;
}