本地时间转换为时间戳

例如现在有个字符串时间为:“2020-08-09T06:51:16.798",把它转换为时间戳,搜了半天没搜到好的办法,只能字符串截取取出年月日时分秒。

time_t LocateToTime_t(char *tTime)
{
   struct tm sTm;
   time_t tReturnTime;
   memset(&sTm, 0x00, sizeof(tm));
   string sTime(tTime);
 
   sTm.tm_year = atoi(sTime.substr(0, 4).c_str());
   sTm.tm_mon = atoi(sTime.substr(5, 2).c_str());
   sTm.tm_mday = atoi(sTime.substr(8, 2).c_str());
   sTm.tm_hour = atoi(sTime.substr(11, 2).c_str());
   sTm.tm_min = atoi(sTime.substr(14, 2).c_str());
   sTm.tm_sec = atoi(sTime.substr(17, 2).c_str());
 
   sTm.tm_year -= 1900;
   sTm.tm_mon -= 1;
 
   tReturnTime = mktime(&sTm);

   return tReturnTime;
}
int main()
{
  char  *pData = "2020-08-09T06:51:16.798";
  time_t ti = LocateToTime_t(pData);
  
  return 0;
}
111
原文地址:https://www.cnblogs.com/zwj-199306231519/p/13455924.html