如何以编程方式从一个时区转换时间到另一个用C? [英] How to programatically convert a time from one timezone to another in C?
问题描述
对于GNU 迄今为止的信息页
命令中包含这个例子:
The info pages for the GNU date
command contains this example:
例如,使用GNU 日期
命令你能回答这个问题
现在是什么时候在纽约时
巴黎时钟显示上午6:30十月
31,2004年?使用日期开始
用`TZ =如图欧洲/巴黎'
以下shell成绩单:
For example, with the GNU
date
command you can answer the question "What time is it in New York when a Paris clock shows 6:30am on October 31, 2004?" by using a date beginning with `TZ="Europe/Paris"' as shown in the following shell transcript:
$ export TZ="America/New_York"
$ date --date='TZ="Europe/Paris" 2004-10-31 06:30'
Sun Oct 31 01:30:00 EDT 2004
在这个例子中,'--date'
操作数以自己的TZ
设置,从而使操作数的其余
根据处理
欧洲/巴黎的规则,对待
字符串 2004-10-31 06:30
,如果它
在巴黎。然而,由于
在最新的输出
命令
根据总的处理
时区的规则,它使用纽约
时间。 (巴黎是正常6小时
在2004年纽约的前方,但是这
例如指的是一个简短的万圣节
期间,当缺口为5小时。)
In this example, the '--date'
operand begins with its own 'TZ'
setting, so the rest of that operand
is processed according to
'Europe/Paris' rules, treating the
string 2004-10-31 06:30
as if it
were in Paris. However, since the
output of the date
command is
processed according to the overall
time zone rules, it uses New York
time. (Paris was normally six hours
ahead of New York in 2004, but this
example refers to a brief Halloween
period when the gap was five hours.)
我想基本上做到用C编程同样的事情,而不调用日期
计划数百万次。基本上,我正在寻找一种方式,采取一个任意日期和时间在一个时区,并将其转换为等效的日期和时间,直接或通过转换和从UTC另一个时区。我不只要我能使用标准功能(的strftime
/ strptime $操纵他们在乎的输入和输出时间格式C $ C> /
mktime
/等)。
I am trying to accomplish essentially the same thing programatically in C without calling the date
program millions of times. Basically I am looking for a way to take an arbitrary date and time in one timezone and convert it to the equivalent date and time in another timezone either directly or via conversion to and from UTC. I don't care about the formats of the input and output time as long as I can manipulate them using standard functions (strftime
/strptime
/mktime
/etc).
的日期
程序似乎做到这一点使用内部的的coreutils
封装复杂的程序,我找了一个方式使用标准POSIX / Linux的程序或外部库做这在C。我看着区信息颇有几分这似乎很有前途,但我找不到任何库做任何有用的事情。
The date
program appears to accomplish this using complex routines internal to the coreutils
package, I am looking for a way to do this in C using either standard POSIX/Linux routines or an external library. I looked at zoneinfo quite a bit which seemed promising but I cannot find any libraries to do anything useful with it.
推荐答案
我想出了似乎与glibc的Linux上工作的一个解决方案,我不知道该怎么移植这种行为,可移植性或更好的任何意见的方式去这将是受欢迎的。
I came up with a solution that seems to work on Linux with glibc, I don't know how portable this behavior is, any comments about portability or a better way to go about this would be welcome.
convert_time.c(没有错误为清晰起见检查):
convert_time.c (No error checking for clarity):
#define _XOPEN_SOURCE
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
struct tm mytm = {0};
time_t mytime;
char buf[100];
mytm.tm_isdst = -1;
putenv(argv[1]);
tzset();
strptime(argv[2], "%Y-%m-%d %H:%M", &mytm);
mytime = mktime(&mytm);
putenv(argv[3]);
tzset();
localtime_r(&mytime, &mytm);
strftime(buf, 100, "%a, %d %b %Y %H:%M:%S %z(%Z)", &mytm);
puts(buf);
return 0;
}
第一个参数是源时区(实际上是TZ =时区传传给putenv),第二个参数是指定格式的时候,最后一个参数是目的地时区。我使用支持使用数据库时区信息glibc哪个时区信息时区的名称。
The first argument is the source timezone (actually "TZ=Timezone" to pass to putenv), the second argument is the time in the specified format, the last argument is the destination timezone. I am using zoneinfo timezone names which glibc supports using the zoneinfo database.
若干DST测试角案件结果对应于从相当于日期
命令以及的它利用区信息数据库,这网站:
The results of several DST test corner cases correspond to the results from the equivalent date
command as well as this site which uses the zoneinfo database:
$ ./convert_time "TZ=America/New_York" "2005-05-31 06:30" "TZ=America/Indiana/Indianapolis"
Tue, 31 May 2005 05:30:00 -0500(EST)
$ ./convert_time "TZ=America/New_York" "2006-05-31 06:30" "TZ=America/Indiana/Indianapolis"
Wed, 31 May 2006 06:30:00 -0400(EDT)
$ ./convert_time "TZ=Europe/Paris" "2004-10-30 06:30" "TZ=America/New_York"
Sat, 30 Oct 2004 00:30:00 -0400(EDT)
$ ./convert_time "TZ=Europe/Paris" "2004-10-31 06:30" "TZ=America/New_York"
Sun, 31 Oct 2004 01:30:00 -0400(EDT)
$ ./convert_time "TZ=Europe/Paris" "2004-11-01 06:30" "TZ=America/New_York"
Mon, 01 Nov 2004 00:30:00 -0500(EST)
这篇关于如何以编程方式从一个时区转换时间到另一个用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!