在 C 中操作时间(时区之间)的一般方法? [英] General way to manipulate the times (between timezones) in C?

查看:40
本文介绍了在 C 中操作时间(时区之间)的一般方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为有关时区之间转换的问题编写示例代码后,其中之一对它的评论是需要更通用的方法来将时区 A 转换为时区 B.我也很好奇自己有一个更高级的原语来进行这样的操作,所以我写了下面的代码.

After writing a sample code for the question about converting between timezones, one of the comments to it was the need for more general method for converting from timezone A to timezone B. I was curious myself too to have a more high-level primitives for such a manipulations, so I wrote the below code.

我看到的一个缺点是它不断摆动环境变量中的 TZ,改变了本地时间"的概念.虽然它似乎有效(虽然我没有检查它对 DST 时期的反应,但由于它基于 Olson 数据库,大概应该),我很好奇是否有人可能对如何处理这个有更好的想法任务 ?

One drawback I see is that it constantly wiggles the TZ in the environment variables, changing the notion of 'local time'. While it seems to work (though I did not check on how it react to the DST periods, but since it's based on the Olson database, presumably it should), I was curious if anyone might have some better ideas on how to deal with this task ?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>

time_t utc_now() {
  struct timeval tv_utc;
  gettimeofday(&tv_utc, NULL);
  return tv_utc.tv_sec;
}

void use_tz(char *timezone) {
  if(timezone) {
    setenv("TZ", timezone, 1);
  } else {
    unsetenv("TZ");
  }
  tzset();
}

time_t utc_from_local_tm(struct tm *local_tm, char *timezone) {
  time_t utc;
  use_tz(timezone);
  utc = mktime(local_tm);
  return utc;
}

struct tm *local_tm_from_utc(time_t utc, char *timezone) {
  use_tz(timezone);
  return localtime(&utc);
}

int main(int argc, char *argv[]) {
  struct tm *tm;
  struct tm tm2;
  time_t utc, utc2, utc3;
  utc = utc_now();
  tm = local_tm_from_utc(utc, "Europe/Brussels");
  printf("Local time in Brussels now: %s", asctime(tm));
  utc2 = utc_from_local_tm(tm, "Europe/Moscow");
  tm = local_tm_from_utc(utc2, "UTC");
  printf("UTC time if the above was the Moscow local time: %s", asctime(tm));

  memset(&tm2, sizeof(tm2), 0);
  /* 13:00:00 on 11 dec 2010 */
  tm2.tm_sec = tm2.tm_min = 0;
  tm2.tm_hour = 13;
  tm2.tm_mon = 11;
  tm2.tm_mday = 11;
  tm2.tm_year = 110;


  utc3 = utc_from_local_tm(&tm2, "Europe/Brussels");
  printf("Brussels time: %s", asctime(&tm2));
  tm = local_tm_from_utc(utc3, "Europe/Moscow");
  printf("At 13:00:00 on 11 dec 2010 CET the time in Moscow will be: %s", asctime(tm));

  exit(0);
}

推荐答案

如果将 TZ 信息存储在环境变量中会让您感到困扰,那么如何为 TZ 创建一个包含 struct tm 和 char* 的新结构体信息?

If storing the TZ information in an environment variable bugs you, then how about creating a new struct that contains both struct tm as well as a char* for the TZ information?

我被宠坏了,因为 R 在这些方面做得很好:

I am spoiled because R does these things quite well:

R> now <- Sys.time()
R> now
[1] "2009-08-01 17:19:07 CDT"
R> format(now, tz="Europe/Brussels")
[1] "2009-08-02 00:19:07"
R> 

它对标准 POSIX 函数有一些扩展/替换,请参阅文件 R-2.9.1/src/main/datetime.c(其中 R-2.9.1 是当前版本)

It has a few extension / replacements to the standard POSIX functions, see the file R-2.9.1/src/main/datetime.c (where R-2.9.1 is the current release)

这篇关于在 C 中操作时间(时区之间)的一般方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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