我无法找到一种方法来从如下字符串中解析时区:“Thu, 1 Sep 2011 09:06:03 -0400 (EDT)”
在我的程序的更大方案中,我需要做的是接收一个 char* 并将其转换为 time_t。以下是我编写的一个简单的测试程序,试图弄清楚 strptime 是否完全考虑了时区,但它似乎不是(当这个测试程序执行时,所有打印的数字都相同,而它们应该不同)。建议?
我还尝试使用 GNU getdate 和 getdate_r,因为这对于可能的灵活格式看起来是一个更好的选择,但是我从编译器收到了“隐式函数声明”警告,暗示我没有包含正确的库。还有什么我应该 #include-ing 来使用 getdate 吗?
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include <stdio.h>
#ifndef __USE_XOPEN
#define __USE_XOPEN
#endif
#include <time.h>
int main (int argc, char** argv){
char *timestr1, *timestr2, *timestr3;
struct tm time1, time2, time3;
time_t timestamp1, timestamp2, timestamp3;
timestr1 = "Thu, 1 Sep 2011 09:06:03 -0400 (EDT)"; // -4, and with timezone name
timestr2 = "Thu, 1 Sep 2011 09:06:03 -0000"; // -0
strptime(timestr1, "%a, %d %b %Y %H:%M:%S %Z", &time1); //includes UTC offset
strptime(timestr2, "%a, %d %b %Y %H:%M:%S %Z", &time2); //different UTC offset
strptime(timestr1, "%a, %d %b %Y %H:%M:%S", &time3); //ignores UTC offset
time1.tm_isdst = -1;
timestamp1 = mktime(&time1);
time2.tm_isdst = -1;
timestamp2 = mktime(&time2);
time3.tm_isdst = -1;
timestamp3 = mktime(&time3);
printf("Hello \n");
printf("%d\n%d\n%d\n", timestamp1, timestamp2, timestamp3);
printf("Check the numbers \n");
return 0;
}