12 char *__tzname[2] = { 0, 0 };
15 weak_alias(__timezone, timezone);
16 weak_alias(__daylight, daylight);
17 weak_alias(__tzname, tzname);
19 static char std_name[TZNAME_MAX+1];
20 static char dst_name[TZNAME_MAX+1];
22 /* all elements are zero-based */
28 } __dst_start, __dst_end;
30 static void zname(char *d, char **s)
33 for (i=0; i<TZNAME_MAX && isalpha(d[i]=**s); i++, (*s)++);
37 static int hhmmss(char **s)
39 int ofs = strtol(*s, s, 10)*3600;
41 if (**s == ':') ofs += strtol(*s+1, s, 10)*60;
42 if (**s == ':') ofs += strtol(*s+1, s, 10);
44 if (**s == ':') ofs -= strtol(*s+1, s, 10)*60;
45 if (**s == ':') ofs -= strtol(*s+1, s, 10);
50 static int dstrule(struct rule *rule, char **s)
52 if (**s != ',') return -1;
56 rule->day = strtol(*s+1, s, 10)-1;
59 rule->month = strtol(*s+1, s, 10)-1;
60 if (**s != '.' || rule->month < 0 || rule->month > 11)
62 rule->week = strtol(*s+1, s, 10)-1;
63 if (**s != '.' || rule->week < 0 || rule->week > 4)
65 rule->day = strtol(*s+1, s, 10);
66 if (rule->day < 0 || rule->day > 6)
71 rule->day = strtol(*s+1, s, 10);
76 rule->time = hhmmss(s);
77 } else rule->time = 7200;
85 strcpy(std_name, "GMT");
86 strcpy(dst_name, "GMT");
87 __tzname[0] = std_name;
88 __tzname[1] = dst_name;
92 if (!(z = getenv("TZ")) || !isalpha(*z)) return;
95 __timezone = hhmmss(&z);
98 if (dst_name[0]) __daylight=1;
100 __dst_offset = hhmmss(&z) - __timezone;
101 if (z==a) __dst_offset = -3600;
103 if (dstrule(&__dst_start, &z) || dstrule(&__dst_end, &z))
109 static int lock[2], init;
117 static int is_leap(int year)
120 return !(year&3) && ((year%100) || !(year%400));
123 static int cutoff_yday(struct tm *tm, struct rule *rule)
125 static const char days_in_month[] = {31,28,31,30,31,30,31,31,30,31,30,31};
126 static const int first_day[] = {0,31,59,90,120,151,181,212,243,273,304,335};
127 int yday, mday, leap;
129 switch (rule->month) {
131 return rule->day + (tm->tm_mon > 1 && is_leap(tm->tm_year));
135 yday = first_day[rule->month];
136 leap = is_leap(tm->tm_year);
137 if (rule->month > 1 && leap) yday++;
138 mday = (rule->day - (yday + tm->tm_wday - tm->tm_yday) + 1400)%7 + 7*rule->week;
139 if (mday >= days_in_month[rule->month] + (leap && rule->month == 1))
145 struct tm *__dst_adjust(struct tm *tm)
148 int start, end, secs;
149 int after_start, before_end;
151 if (tm->tm_isdst >= 0) return tm;
157 secs = tm->tm_hour*3600 + tm->tm_min*60 + tm->tm_sec;
158 start = cutoff_yday(tm, &__dst_start);
159 end = cutoff_yday(tm, &__dst_end);
161 after_start = (tm->tm_yday > start || (tm->tm_yday == start && secs >= __dst_start.time));
162 before_end = (tm->tm_yday < end || (tm->tm_yday == end && secs < __dst_end.time));
164 if ((after_start && before_end) || ((end < start) && (after_start || before_end))) {
165 tm->tm_sec -= __dst_offset;
167 t = __tm_to_time(tm);
168 return __time_to_tm(t, tm);
169 } else tm->tm_isdst = 0;