Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将字符串(2019年10月3日星期四18:45:10)转换为cpp中的时间格式?_C++ - Fatal编程技术网

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

C++ 如何将字符串(2019年10月3日星期四18:45:10)转换为cpp中的时间格式?,c++,C++,将该时间格式存储在其他文件中。并将其作为字符串检索。如何再次将其更改为时间格式。您可以使用strtime解析时间,然后mktime将其转换为时间 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

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

您可以使用
strtime
解析时间,然后
mktime
将其转换为时间

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);

您可以在

找到输入字段描述符的详细用法,我试过了。但是不起作用“不起作用”是什么意思?有错误或输出吗?请分享我的代码:struct tm tim;字符*当前时间;time_t tm=时间(空);当前时间=strtok(ctime(&tm),“\n”);当inputtime(NULL)返回当前时间时,cout输出会因相同的inputtime而变化。您当前的时间正在改变。因此,产出也会发生变化。