Tzset and daily global interpretation of variables in time. h

The time.h header for the global daylight variable says: “This variable has a non-zero value if daylight saving time rules apply. A value other than zero does not necessarily mean that daylight saving time is valid, it means that sometimes a transition is valid summer time. "

Now I noticed that on Solaris 11.2 and Linux, the daylight variable is set to 1, although my time zone does not use daylight savings at all (Australia / Brisbane).

The code example confirms this, if I run tzset and output global variables, we get: daylight = 1 tz [0] = [AEST] tz [1] = [AEDT] time zone = [-36000]

But, in my opinion, daylight should be set to 0, since my zone does not have daylight saving time at any time of the year.

I also noticed that struct tm, when the current time is set, returns tm_isdst = 0, which is true.

So why is the daylight variable set to 1? Shouldn't it be 0? Or am I misinterpreting this?

Code:

#include <stdio.h>
#include <time.h>
void main()
{
  time_t t;
  struct tm     *tms = { 0 };
  tzset();
  time(&t);
  tms = localtime(&t);
  printf("date and time : %s",ctime(&t));
  printf("daylight = %d tz[0] = [%s] tz[1] = [%s] timezone = [%ld]\n", daylight, tzname[0], tzname[1], timezone);
  printf("tm_isdst = %d\n",tms->tm_isdst);
}

Exit:

date and time : Mon Nov 30 16:41:01 2015
daylight = 1 tz[0] = [AEST] tz[1] = [AEDT] timezone = [-36000]
tm_isdst = 0
+6
source share
3 answers

Member's C standard tm_isdst.

The value tm_isdstis positive if daylight saving time is in effect, and zero if daylight saving time is not in effect, and negative if information is not available. C11dr §7.27.1 4

* nix * nix daylight.
daylight C.


gnu.org

: int daylight
, . , ; , .


tm_isdst struct tm. , DST .

daylight != 0 , DST .

/ (@Jon Skeet) 1972 , daylight == 1 , daylight , (, 1970 ),

"... " .


, DST ( , ) 1970 "/".

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

int main(void) {
  setenv("TZ", "Australia/Brisbane", 1);
  tzset();
  time_t now;
  time(&now);
  struct tm tm;
  int isdst = 42; // See Hitchhiker's_Guide_to_the_Galaxy
  time_t t;
  for (t = 0; t < now; t += 3600) {
    tm = *localtime(&t);
    if (tm.tm_isdst != isdst) {
      printf("dst:%d %s", tm.tm_isdst, ctime(&t));
      isdst = tm.tm_isdst;
    }
  }
  printf("dst:%d %s", tm.tm_isdst, ctime(&t));
  return 0;
}

dst:0 Thu Jan  1 10:00:00 1970
dst:1 Sun Oct 31 03:00:00 1971
dst:0 Sun Feb 27 02:00:00 1972
dst:1 Sun Oct 29 03:00:00 1989
dst:0 Sun Mar  4 02:00:00 1990
dst:1 Sun Oct 28 03:00:00 1990
dst:0 Sun Mar  3 02:00:00 1991
dst:1 Sun Oct 27 03:00:00 1991
dst:0 Sun Mar  1 02:00:00 1992
dst:0 Tue Dec  1 16:00:00 2015
+6

/ , ; australasia, , DST.

daylight , , - ( - ) . , 1, , 0, UTC.

( , , DST, UTC, daylight 1 . , , ...)

+3

, tzset() - . TZ, zoneinfo . , tzset() , , - , , , , . , gmtime(), localtime(), mktime(), . tzset() . tzset() , , , "". , - , tz [1].

, , , tzset() / , , , tzset(), , .

" " " " , , , TZ, , , " " 1- 3- ". , , TZ, . , zoneinfo , , tzset() . : localtime() .. , tzset() , . " " : tzset() - /, " ", , , , tz [1] . , , . , , .

0
source

Source: https://habr.com/ru/post/1617938/


All Articles