gpt4 book ai didi

c++ - 如何将包含时间的字符串变量转换为 c++ 中的 time_t 类型?

转载 作者:IT老高 更新时间:2023-10-28 12:36:31 26 4
gpt4 key购买 nike

我有一个字符串变量,其中包含 hh:mm:ss 格式的时间。如何将其转换为 time_t 类型?例如:字符串 time_details = "16:35:12"

另外,如何比较两个包含时间的变量,以确定哪个是最早的?例如:字符串 curr_time = "18:35:21" 字符串 user_time = "22:45:31"

最佳答案

使用 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_timestrftime供引用

关于c++ - 如何将包含时间的字符串变量转换为 c++ 中的 time_t 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11213326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com