我有一个包含hh:mm:ss格式的时间的字符串变量。如何将其转换为time_t类型?例如:string time_details =“ 16:35:12”另外,如何比较两个包含时间的变量,以便确定哪个是最早的?例如:string curr_time =“ 18:35:21” string user_time =“ 22:45:31”
3 回答
![?](http://img1.sycdn.imooc.com/545863cd0001b72a02200220-100-100.jpg)
狐的传说
TA贡献1804条经验 获得超3个赞
您可以使用strptime(3)解析时间,然后mktime(3)将其转换为time_t:
const char *time_details = "16:35:12";
struct tm tm;
strptime(time_details, "%H:%M:%S", &tm);
time_t t = mktime(&tm); // t is now your desired time_t
![?](http://img1.sycdn.imooc.com/54584cfb0001308402200220-100-100.jpg)
慕村9548890
TA贡献1884条经验 获得超4个赞
使用C ++ 11,您现在可以执行
struct std::tm tm;
std::istringstream ss("16:35:12");
ss >> std::get_time(&tm, "%H:%M:%S"); // or just %T in this case
std::time_t time = mktime(&tm);
请参阅std :: get_time和strftime以供参考
- 3 回答
- 0 关注
- 807 浏览
添加回答
举报
0/150
提交
取消