我如何在格林尼治标准​​时间time_t的Windows上使用C [英] How do I get time_t in GMT on Windows in C

查看:589
本文介绍了我如何在格林尼治标准​​时间time_t的Windows上使用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些code,将多个内部连通系统上运行。我使用的时间()得到time_t的,但这导致与系统之间的时差问题,所以我想在time_t的格林尼治标准​​时间。我一直在寻找通过time.h中的功能,但我不清楚我怎么能肯定,我会得到正确的时间。这是我想出迄今:

I am writing some code that will run on multiple intercommunicating systems. I was using time() to get time_t, but this was causing problems with time zone differences between the systems, so I want to get time_t in GMT. I've been looking through the time.h functions, but it is unclear to me how I can be sure that I will get the time correctly. This is what I've come up with so far:

time_t t = time();
struct tm *gtm = gmtime(&t);
time_t gt = mktime(gtm);

现在,这似乎让我的机器上正确的答案,但我想知道它是否会奏效普遍之前,我推了,即使其它机器的时钟设置为本地时间或GMT,或在不同的时间区或别的什么东西。我担心的原因是因为mktime的。在说明书中,它说,它除$ P $点的tm结构为日历时间前$ P $在当地时间pssed。这听起来好像也不会返回GMT时间,虽然这似乎是我的机器上。此外,当我打印出来的GT,它是领先的T 4个小时,这似乎是正确的。但是,如果我运行以下命令:

Now, this seems to get the correct answer on my machine, but I want to know if it will work universally before I push it out, even if the other machines' clocks are set to local time or GMT or in different time zones or whatever. The reason I am concerned is because of mktime. In the description, it says that it interprets the tm struct as "a calendar time expressed in local time." That sounds to me like it will not be returning the GMT time, though it seems to be on my machine. Also, when I print out gt, it is 4 hours ahead of t, which seems right. But if I run the following:

time_t t = time();
struct tm *gtm = gmtime(&t);
struct tm *ltm = localtime(&t);
printf("%d, %d\n", gtm->tm_hour, ltm->tm_hour);

的时间是相同的,都是本地时间,这是不是我的预期。

the hours are the same and are local time, which is not what I expected.

有关的记录,在另一个答案,我看到了参考timegm(),它听起来很完美,但它不是我的系统上。

For the record, in another answer, I saw reference to timegm(), which sounds perfect, but it does not exist on my system.

因此​​,在短期,我怎么得到time_t的格林尼治标准​​时间任何Windows机器上用C?

So in short, how do I get time_t in GMT on any Windows machine in C?

编辑:已添加,因为我没有使用MSVCRT删除MSVCRT标签

removed msvcrt tag that was added, as I am not using the msvcrt.

推荐答案

time_t的总是UTC,顾名思义。所以,时间()你想要做什么。时区才开始发挥作用,当你的time_t和分解时间再presentation之间的转换。

time_t is always in UTC, by definition. So time() does what you want. Timezones only come into play when you are converting between time_t and broken-down time representation.

如果您在破旧的时间再presentation UTC时间,你需要暂时切换时区为UTC,使用mktime()根据转换为time_t的,并且切换时区回来了,就像这样:

If you have the time in UTC in broken-down time representation, you need to temporarily switch the timezone to UTC, use mktime() to convert to a time_t, and switch the timezone back, like this:

time_t convert_utc_tm_to_time_t (struct tm *tm)
{
    char *tz;
    time_t result;

    /* temporarily set timezone to UTC for conversion */
    tz = getenv("TZ");
    if (tz) {
      tz = strdup (tz);
      if (!tz) {
        // out of memory
        return -1;
      }
    }
    setenv("TZ", "", 1);
    tzset();

    result = mktime (tm);

    /* restore timezone */
    if (tz) {
      setenv("TZ", tz, 1);
      free (tz);
    }
    else {
      unsetenv("TZ");
    }
    tzset();

    return result;
}

这篇关于我如何在格林尼治标准​​时间time_t的Windows上使用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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