c 中的 strptime 与时区偏移

发布于 2024-12-03 09:18:23 字数 1354 浏览 1 评论 0原文

我无法找到从字符串中解析时区的方法,如下所示: “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;
}

I'm having trouble finding a way to parse the timezone out of strings like the following:
"Thu, 1 Sep 2011 09:06:03 -0400 (EDT)"

What I need to do in the larger scheme of my program is take in a char* and convert it to a time_t.
The following is a simple test program I wrote to try and figure out if strptime was accounting for timezone at all, and it doesn't appear to be (when this test program executes all the printed numbers are the same when they should differ). Suggestions?

I also tried to use the GNU getdate and getdate_r, because that looks like a better option for possibly flexible formats, but I got a "implicit function declaration" warning from the compiler, implying I wasn't including the correct libraries. Is there something else I should be #include-ing to use 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒人说梦 2024-12-10 09:18:23

在 MacOS X (10.7.1) 上,strptime(3) 手册页中列出的“错误”之一是:

%Z 格式说明符仅接受本地时区的时区缩写或值
“格林威治标准时间”。此限制是由于时区缩写过多而导致歧义。
其中一个例子是 EST,它既是东部标准时间,又是澳大利亚东部夏令时间。

前段时间,我写了一对命令——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),所以这些命令显示的是(如手册页所述),< code>strptime() 识别 PDT 和 GMT 并为每个提供适当的答案。 timestamp-u 选项表示“打印 UTC(又名 GMT)时间”而不是本地时间。 UTC 晚上 9 点实际上是 PDT 下午 2 点。

如果您想要来源,请联系我 - 请参阅我的个人资料。

On MacOS X (10.7.1), one of the 'bugs' listed in the strptime(3) manual page is:

The %Z format specifier only accepts time zone abbreviations of the local time zone, or the value
"GMT". This limitation is because of ambiguity due to of the over loading of time zone abbreviations.
One such example is EST which is both Eastern Standard Time and Eastern Australia Summer Time.

Some time ago, I wrote a pair of commands - timestamp (May 1989 for revision 1.1) and strptime (July 2007 for revision 1.1). These are relatively thin covers over the strftime() and strptime() functions.

Running them on MacOS X, I get:

$ 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
$

I'm in US/Pacific (or America/Los_Angeles, or Pacific Daylight Time; UTC-07:00), so what these commands show is that (as the manual page says), strptime() recognizes PDT and GMT and gives appropriate answers for each. The -u option to timestamp indicates 'print UTC (aka GMT) time' rather than local time. And 9 pm in UTC is indeed 2 pm in PDT.

Contact me if you would like the source - see my profile.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文