#include <stdio.h>
#include <time.h>
#include <string.h>
void main() {
struct tm tm;
memset (&tm, '\0', sizeof(tm));
if (!strptime ("15/Sep/2014:16:00:00 +0300", "%d/%b/%Y:%H:%M:%S%t%z", &tm)) {
perror("strptime");
return;
}
time_t gmt = timegm (&tm); // or mktime(), not relevant here
printf("%d gmtoff=%lu %s %lu\n", tm.tm_hour, tm.tm_gmtoff, tm.tm_zone, gmt);
}
16 gmtoff=0 GMT 1410796800
我预计gmtoff为3。
这就像strptime以某种方式解析偏移,因为它成功,但它不补在结构中偏移。我已经尝试过任何形式的字符串和日期时间字符串的变体,但没有运气。
根据http://lxr.devzen.net/source/xref/glibc/time/strptime_l.c#751应该修改struct字段。
调用'timegm()'之前'tm.tm_gmtoff' *的值是多少? – unwind 2014-10-01 09:42:50
您正在使用memset来初始化包含任意数据的数据结构。虽然字符文字''\ 0''应该等于整数值'0'(转换后),但我认为使用实际整数值'0'而不是字符文字更明智。 – 2014-10-01 09:46:13
@unwind 10800,很好的接...所以我应该使用mktime(),我错误地认为tm没有被timegm感动。现在我必须理解为什么mktime()在我的程序中泄漏,而使用timegm()不会。 – lethalman 2014-10-01 09:52:33