How to convert a string variable containing time to time_t type in c++?(如何将包含时间的字符串变量转换为 C++ 中的 time_t 类型?)
问题描述
我有一个包含 hh:mm:ss 格式时间的字符串变量.如何将其转换为 time_t 类型?例如:string time_details = "16:35:12"
I have a string variable containing time in hh:mm:ss format. How to convert it into time_t type? eg: string time_details = "16:35:12"
另外,如何比较两个包含时间的变量来决定哪个是最早的?例如:string curr_time = "18:35:21"string user_time = "22:45:31"
Also, how to compare two variables containing time so as to decide which is the earliest? eg : string curr_time = "18:35:21" string user_time = "22:45:31"
推荐答案
您可以使用 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
这篇关于如何将包含时间的字符串变量转换为 C++ 中的 time_t 类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将包含时间的字符串变量转换为 C++ 中的 time_t 类型?


基础教程推荐
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09