gpt4 book ai didi

c - strptime 的问题

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:29 28 4
gpt4 key购买 nike

我正在尝试像这样解析日期时间,

char *line = "Feb 21 20:15:58";
struct tm t;

strptime (line, "%B %d %T", &t);
printf ("%ld\n", mktime (&t));

输出是1,我是不是使用了错误的时间格式?

最佳答案

Year 在您的输入变量 line 中丢失。

此外,来自 Linux man ,

RETURN VALUE

   The return value of the function is a pointer to the first character
not processed in this function call. In case the input string
contains more characters than required by the format string, the
return value points right after the last consumed input character.
In case the whole input string is consumed, the return value points
to the null byte at the end of the string. If strptime() fails to
match all of the format string and therefore an error occurred, the
function returns NULL.

NOTES

   In principle, this function does not initialize tm but stores only
the values specified. This means that tm should be initialized
before the call.

因此,除了向 strptime 提供年份之外,您还应该检查返回值,并且在调用该函数之前还应该初始化 tm t 结构。

关于c - strptime 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42358914/

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