且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用C代码获取相对于UTC的以分钟为单位的本地时间偏移量?

更新时间:2022-12-23 20:44:15

我想对此问题提交另一个答案,即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;
}