Why does glibc “timezone” global not agree with system time on DST?

半世苍凉 提交于 2019-12-04 06:27:31

I don't think "timezone" changes with daylight time. Try the "daylight" variable. On my system:

      The external variable timezone contains the difference, in seconds,
      between UTC and local standard time (for example, in the U.S. Eastern
      time zone (EST), timezone is 5*60*60).  The external variable daylight
      is non-zero only if a summer time zone adjustment is specified in the
      TZ environment variable.

Look at tm.tm_isdst field after doing this:

  time_t current_time;
  struct tm tm;

  current_time = time(NULL);
  localtime_r(&current_time, &tm);

According to the localtime_r(3) manpage, this does actually indicate whether DST is in effect at the time specified. I think you then need to assume that DST adds one hour to the timezone(3) variable you're already using, or do the diff trick against GMT.

Works for me in Australian AEST, hope it works for you.

You can use tm_gmtoff mem ber of struct tm which is same as ::timezone but it considers DST and the sign is reversed.

http://www.gnu.org/s/libc/manual/html_node/Time-Zone-Functions.html#Time-Zone-Functions

Here is my code for this using tm_gmtoff if linux is defined, and using timezone.tz_minuteswest from gettimofday otherwise (here 'ltm' is the output of localtime):

{
    int tz_offset;

#if defined(__linux__)
    tz_offset= ltm.tm_gmtoff;
#else
    tz_offset= -tz.tz_minuteswest*60 + ltm.tm_isdst*3600;
#endif

     printf ("LT = UTC +d sec\n", tz_offset);
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!