#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