如何将字符串(2019年10月3日星期四18:45:10)转换为cpp中的time_t格式?

How to convert string ( Thu Oct 3 18:45:10 2019) to time_t format in cpp?

本文关键字:cpp 转换 中的 time 格式 字符串 2019年 10月 星期四 3日      更新时间:2023-10-16

将该时间格式存储在其他文件中。并将其检索为字符串。如何再次将其更改为时间格式。

您可以使用strptime来分析时间,然后mktime将其转换为time_t

const char *time_details = "Thu Oct 3 18:45:10 2019";
struct tm tm;
// %a or %A
// The weekday name according to the current locale, in abbreviated form or the full name.
// %b or %B or %h
// The month name according to the current locale, in abbreviated form or the full name.

// the field descriptors must match the string format you are providing. 
strptime(time_details, "%a %b %d %H:%M:%S %Y", &tm);
time_t t = mktime(&tm);

您可以在 strptime(3( 找到输入字段描述符的详细用法