Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带时区偏移的c中的strTime_C_Timezone_Getdate_Strptime_Timezone Offset - Fatal编程技术网

带时区偏移的c中的strTime

带时区偏移的c中的strTime,c,timezone,getdate,strptime,timezone-offset,C,Timezone,Getdate,Strptime,Timezone Offset,我很难找到从字符串中解析时区的方法,如下所示: 2011年9月1日星期四09:06:03-0400(美国东部时间) 在我的程序的更大方案中,我需要做的是接收一个字符*并将其转换为时间。 下面是我编写的一个简单的测试程序,它试图弄清楚strtime是否考虑了时区,但它似乎没有(当这个测试程序执行时,所有打印的数字都是相同的,而它们应该是不同的)。建议 我还尝试使用GNU getdate和getdate\r,因为对于可能灵活的格式来说,这看起来是一个更好的选择,但是我从编译器那里得到了一个“隐式函数

我很难找到从字符串中解析时区的方法,如下所示: 2011年9月1日星期四09:06:03-0400(美国东部时间)

在我的程序的更大方案中,我需要做的是接收一个字符*并将其转换为时间。 下面是我编写的一个简单的测试程序,它试图弄清楚strtime是否考虑了时区,但它似乎没有(当这个测试程序执行时,所有打印的数字都是相同的,而它们应该是不同的)。建议

我还尝试使用GNU getdate和getdate\r,因为对于可能灵活的格式来说,这看起来是一个更好的选择,但是我从编译器那里得到了一个“隐式函数声明”警告,这意味着我没有包含正确的库。我是否还应该使用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;
}
#包括
#包括
#包括
#包括
#如果没有,请使用打开
#定义\使用\打开
#恩迪夫
#包括
int main(int argc,字符**argv){
字符*timestr1、*timestr2、*timestr3;
struct tm time1、time2、time3;
时间t timestamp1、timestamp2、timestamp3;
timestr1=“Thu,2011年9月1日09:06:03-0400(EDT)”;//-4,并带有时区名称
timestr2=“Thu,2011年9月1日09:06:03-0000”;//-0
strtime(timestr1、%a、%d%b%Y%H:%M:%S%Z、&time1);//包括UTC偏移量
strtime(timestr2、%a、%d%b%Y%H:%M:%S%Z、&time2);//不同的UTC偏移量
strtime(timestr1、%a、%d%b%Y%H:%M:%S、&time3);//忽略UTC偏移量
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(“检查数字”);
返回0;
}
在MacOS X(10.7.1)上,
strtime(3)
手册页面中列出的“bug”之一是:

%Z格式说明符仅接受本地时区或值的时区缩写 “格林尼治标准时间”。这一限制是由于时区缩写的过度加载导致的歧义。 其中一个例子是东部标准时间和澳大利亚东部夏季时间

不久前,我编写了一对命令——timestamp(1989年5月用于修订版1.1)和strptime(2007年7月用于修订版1.1)。这些是对
strftime()
strptime()
函数相对较薄的覆盖

在MacOS X上运行它们,我得到:

$ strptime -T '%Y-%m-%dT%H:%M:%S %Z' 2011-09-08T21:00:00PDT
1315540800 = 2011-09-08T21:00:00PDT
$ timestamp 1315540800
1315540800 = Thu Sep 08 21:00:00 2011
$ timestamp -u 1315540800
1315540800 = Fri Sep 09 04:00:00 2011
$ strptime -T '%Y-%m-%dT%H:%M:%S %Z' 2011-09-08T21:00:00GMT
1315515600 = 2011-09-08T21:00:00GMT
$ timestamp 1315515600
1315515600 = Thu Sep 08 14:00:00 2011
$ timestamp -T '%Y-%m-%dT%H:%M:%S %z' 1315515600
1315515600 = 2011-09-08T14:00:00 -0700
$
我在美国/太平洋(或美国/洛杉矶,或太平洋夏令时;UTC-07:00),因此这些命令显示的是(如手册页面所示),
strtime()
识别PDT和GMT,并为每种情况提供适当的答案。时间戳的
-u
选项表示“打印UTC(也称GMT)时间”,而不是本地时间。UTC的晚上9点实际上是PDT的下午2点


如果您想要源代码,请与我联系-查看我的个人资料。

我不确定您在定义
\uuu USE XOPEN
后是否应该使用
\endif
指令,请尝试将其放在
\include
之后。您永远不应该定义
\uu USE XOPEN
。这是glibc头实现的内部宏,根据
\u XOPEN\u SOURCE
宏的存在/值自动定义。@andrew更改#endif的顺序似乎也没有什么区别way@R.诚然,在测试程序中,如果我拿出“使用”笔,它不会抱怨,但由于某种原因,在我正在处理的实际程序的上下文中,如果没有“使用”XOPEN,我会在与上面程序中使用的调用完全相同的调用中收到编译器警告“函数'strtime'的隐式声明”。为什么会这样?在执行任何
\include
指令之前,请确保您没有定义任何功能测试宏(*\u SOURCE),或者在源代码顶部至少定义了
\XOPEN\u SOURCE
值,至少为600(最好为700)。