用C代码获取相对于UTC的以分钟为单位的本地时间偏移量? [英] C code to get local time offset in minutes relative to UTC?

查看:132
本文介绍了用C代码获取相对于UTC的以分钟为单位的本地时间偏移量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有找到一种简单的方法来获取本地时间和UTC时间之间以分钟为单位的时间偏移量.

I didn't find a trivial way to get the time offset in minutes between the local time and the UTC time.

起初我打算使用tzset(),但是它不提供夏令时.根据手册页,如果实行日光节约,则它只是一个零差的整数.通常是一个小时,但在某些国家可能是半小时.

At first I intended to use tzset() but it doesn't provide the daylight saving time. According to the man page, it is simply an integer different of zero if day light saving is in effect. While it is usually an hour, it may be half an hour in some country.

我宁愿避免计算gmtime()localtime()返回的当前UTC之间的时间差.

I would prefer avoiding to compute the time difference between current UTC returned by gmtime() and localtime().

一个更通用的解决方案将为我提供指定位置和正time_t值(或至少在本地)的此信息.

A more general solution would give me this information for a specified location and a positive time_t value, or at least locally.

编辑1 :用例是为 https:获取正确的本地时间偏移: //github.com/chmike/timez . 顺便说一句,如果您认为操作时间的libc函数还可以,请阅读此 https://rachelbythebay .com/w/2013/03/17/time/.

Edit 1: the use case is to get the right local time offset for https://github.com/chmike/timez. BTW, If you thought libc functions to manipulate time were Ok, read this https://rachelbythebay.com/w/2013/03/17/time/.

编辑2 :到目前为止,以分钟为单位计算到UTC的时间偏移量的最佳,最简单的解决方案是

Edit 2: the best and simplest solution I have so far to compute the time offset to UTC in minutes is

// Bogus: assumes DST is always one hour
tzset();
int offset = (int)(-timezone / 60 + (daylight ? 60 : 0));

问题在于确定实际的日光节约时间.

The problem is to determine the real day light saving time.

编辑3 :受 @trenki 的回答启发,我想到了以下解决方案.这是一种黑客行为,它会诱骗mktime()gmtime()的输出视为本地时间.当DST更改在UTC时间和本地时间之间的时间范围内时,结果将不准确.

Edit 3: Inspired by the answer of @trenki, I came up with the following solution. This is a hack in that it tricks mktime() to consider the output of gmtime() as the localtime. The result is inaccurate when the DST change is in the time span between UTC time and localtime.

#include <stdio.h>
#include <time.h>

int main()
{
    time_t rawtime = time(NULL);
    struct tm *ptm = gmtime(&rawtime);
    // Request that mktime() looksup dst in timezone database
    ptm->tm_isdst = -1;                
    time_t gmt = mktime(ptm);
    double offset = difftime(rawtime, gmt) / 60;
    printf("%f\n", offset);
    return 0;
}

推荐答案

我想对此问题提交另一个答案,即AFAICS也处理IDL.

I would like to submit yet another answer to this question, one that AFAICS also deals with the IDL.

此解决方案取决于timegmmktime.在Windows上,CRT中的timegm作为_mkgmtime可用,换句话说,定义了条件宏.

This solution depends on timegm and mktime. On Windows timegm is available as _mkgmtime from the CRT, in other words define a conditional macro.

#if _WIN32
#    define timegm _mkgmtime
#endif

int local_utc_offset_minutes ( ) {
    time_t t  = time ( NULL );
    struct tm * locg = localtime ( &t );
    struct tm locl;
    memcpy ( &locl, locg, sizeof ( struct tm ) );
    return (int)( timegm ( locg ) - mktime ( &locl ) ) / 60;
}

这篇关于用C代码获取相对于UTC的以分钟为单位的本地时间偏移量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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