我有一個包含hh:mm:ss格式的時間的字符串變量。如何將其轉(zhuǎn)換為time_t類型?例如:string time_details =“ 16:35:12”另外,如何比較兩個包含時間的變量,以便確定哪個是最早的?例如:string curr_time =“ 18:35:21” string user_time =“ 22:45:31”
3 回答

狐的傳說
TA貢獻1804條經(jīng)驗 獲得超3個贊
您可以使用strptime(3)解析時間,然后mktime(3)將其轉(zhuǎn)換為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

慕村9548890
TA貢獻1884條經(jīng)驗 獲得超4個贊
使用C ++ 11,您現(xiàn)在可以執(zhí)行
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 關(guān)注
- 832 瀏覽
添加回答
舉報
0/150
提交
取消