如何在C中将一个时区从一个时区转换到另一个时区? [英] How to programatically convert a time from one timezone to another in C?

查看:161
本文介绍了如何在C中将一个时区从一个时区转换到另一个时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GNU date 命令的信息页包含此示例:

The info pages for the GNU date command contains this example:


例如,使用GNU 日期
命令,您可以回答问题
当纽约时间是一个
巴黎时钟显示在十月的六月三十日
二零零四年三月三十一日?通过使用
开头的日期
与TZ =Europe / Paris'如
所示,以下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
设置,因此该操作数
的其余部分根据
'Europe / Paris'规则进行处理,处理
字符串 2004-10-31 06:30 好像在
在巴黎。然而,由于 date 命令的
输出是根据
的总时间规则处理的
,所以它使用纽约
时间。 (巴黎通常是2004年纽约纽约前6小时
,但是这个
的例子是一个简短的万圣节
期间,差距是五个小时。)

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 / mktime / etc)。

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).

date 程序似乎使用 coreutils 包内部的复杂例程来完成此任务,我正在寻找一种在C中使用标准POSIX / Linux例程或外部图书馆我看到 zoneinfo 相当有希望,但我找不到任何图书馆做任何有用的事情

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 = Timezone到putenv),第二个参数是指定格式的时间,最后一个参数是目标时区。我正在使用glibc支持使用zoneinfo数据库的zoneinfo时区名称。

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测试角案例的结果对应于等效的日期命令以及这个使用的网站zoneinfo数据库:

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆