【发布时间】:2016-04-26 12:47:03
【问题描述】:
我是 c 新手,正在尝试使用 strptime 函数,它将字符串时间转换为结构 tm。转换后我没有得到正确的时间。一切都很好,但年份显示错误(默认年份 1900)。
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
int main()
{
struct tm tm;
char *pszTemp = "Mon Apr 25 09:53:00 IST 2016";
char szTempBuffer[256];
memset(&tm, 0, sizeof(struct tm));
memset(szTempBuffer, 0, sizeof(szTempBuffer));
strptime(pszTemp, "%a %b %d %H:%M:%S %Z %Y", &tm);
strftime(szTempBuffer, sizeof(szTempBuffer), "%Y-%m-%d %H:%M:%S", &tm);
printf("Last Boot Time after parsed = %s\n", szTempBuffer);
return 0;
}
输出:1900-04-25 09:53:00
【问题讨论】:
-
您检查过
strptime返回的内容吗?这样它就不会返回NULL指针? -
您是否尝试使用
-Wall选项进行编译? -
@LPs:没用。
-
@JoachimPileborg:它返回 NULL。你能说出原因/解决方案吗??
-
你的平台是什么?我在 debian 8.2 gcc 4.9 上试过,一切正常。
标签: c timezone unix-timestamp strptime