📄 tzset.c
字号:
else goto out; if (*tz != '\0' && *tz != '/' && *tz != ',') goto out; else if (*tz == '/') { /* Get the time of day of the change. */ ++tz; if (*tz == '\0') goto out; switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss)) { default: hh = 2; /* Default to 2:00 AM. */ case 1: mm = 0; case 2: ss = 0; case 3: break; } for (l = 0; l < 3; ++l) { while (isdigit (*tz)) ++tz; if (l < 2 && *tz == ':') ++tz; } tzr->secs = (hh * 60 * 60) + (mm * 60) + ss; } else /* Default to 2:00 AM. */ tzr->secs = 2 * 60 * 60; tzr->computed_for = -1; } out: update_vars ();}/* Interpret the TZ envariable. */static voidinternal_functiontzset_internal (always, explicit) int always; int explicit;{ static int is_initialized; register const char *tz; if (is_initialized && !always) return; is_initialized = 1; /* Examine the TZ environment variable. */ tz = getenv ("TZ"); if (tz == NULL && !explicit) /* Use the site-wide default. This is a file name which means we would not see changes to the file if we compare only the file name for change. We want to notice file changes if tzset() has been called explicitly. Leave TZ as NULL in this case. */ tz = TZDEFAULT; if (tz && *tz == '\0') /* User specified the empty string; use UTC explicitly. */ tz = "Universal"; /* A leading colon means "implementation defined syntax". We ignore the colon and always use the same algorithm: try a data file, and if none exists parse the 1003.1 syntax. */ if (tz && *tz == ':') ++tz; /* Check whether the value changed since the last run. */ if (old_tz != NULL && tz != NULL && strcmp (tz, old_tz) == 0) /* No change, simply return. */ return; if (tz == NULL) /* No user specification; use the site-wide default. */ tz = TZDEFAULT; tz_rules[0].name = NULL; tz_rules[1].name = NULL; /* Save the value of `tz'. */ if (old_tz != NULL) free (old_tz); old_tz = tz ? __strdup (tz) : NULL; /* Try to read a data file. */ __tzfile_read (tz, 0, NULL); if (__use_tzfile) return; /* No data file found. Default to UTC if nothing specified. */ if (tz == NULL || *tz == '\0' || (TZDEFAULT != NULL && strcmp (tz, TZDEFAULT) == 0)) { tz_rules[0].name = tz_rules[1].name = "UTC"; tz_rules[0].type = tz_rules[1].type = J0; tz_rules[0].m = tz_rules[0].n = tz_rules[0].d = 0; tz_rules[1].m = tz_rules[1].n = tz_rules[1].d = 0; tz_rules[0].secs = tz_rules[1].secs = 0; tz_rules[0].offset = tz_rules[1].offset = 0L; tz_rules[0].change = tz_rules[1].change = (time_t) -1; tz_rules[0].computed_for = tz_rules[1].computed_for = 0; update_vars (); return; } __tzset_parse_tz (tz);}/* Figure out the exact time (as a time_t) in YEAR when the change described by RULE will occur and put it in RULE->change, saving YEAR in RULE->computed_for. */static voidinternal_functioncompute_change (rule, year) tz_rule *rule; int year;{ register time_t t; if (year != -1 && rule->computed_for == year) /* Operations on times in 2 BC will be slower. Oh well. */ return; /* First set T to January 1st, 0:00:00 GMT in YEAR. */ if (year > 1970) t = ((year - 1970) * 365 + /* Compute the number of leapdays between 1970 and YEAR (exclusive). There is a leapday every 4th year ... */ + ((year - 1) / 4 - 1970 / 4) /* ... except every 100th year ... */ - ((year - 1) / 100 - 1970 / 100) /* ... but still every 400th year. */ + ((year - 1) / 400 - 1970 / 400)) * SECSPERDAY; else t = 0; switch (rule->type) { case J1: /* Jn - Julian day, 1 == January 1, 60 == March 1 even in leap years. In non-leap years, or if the day number is 59 or less, just add SECSPERDAY times the day number-1 to the time of January 1, midnight, to get the day. */ t += (rule->d - 1) * SECSPERDAY; if (rule->d >= 60 && __isleap (year)) t += SECSPERDAY; break; case J0: /* n - Day of year. Just add SECSPERDAY times the day number to the time of Jan 1st. */ t += rule->d * SECSPERDAY; break; case M: /* Mm.n.d - Nth "Dth day" of month M. */ { unsigned int i; int d, m1, yy0, yy1, yy2, dow; const unsigned short int *myday = &__mon_yday[__isleap (year)][rule->m]; /* First add SECSPERDAY for each day in months before M. */ t += myday[-1] * SECSPERDAY; /* Use Zeller's Congruence to get day-of-week of first day of month. */ m1 = (rule->m + 9) % 12 + 1; yy0 = (rule->m <= 2) ? (year - 1) : year; yy1 = yy0 / 100; yy2 = yy0 % 100; dow = ((26 * m1 - 2) / 10 + 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7; if (dow < 0) dow += 7; /* DOW is the day-of-week of the first day of the month. Get the day-of-month (zero-origin) of the first DOW day of the month. */ d = rule->d - dow; if (d < 0) d += 7; for (i = 1; i < rule->n; ++i) { if (d + 7 >= (int) myday[0] - myday[-1]) break; d += 7; } /* D is the day-of-month (zero-origin) of the day we want. */ t += d * SECSPERDAY; } break; } /* T is now the Epoch-relative time of 0:00:00 GMT on the day we want. Just add the time of day and local offset from GMT, and we're done. */ rule->change = t - rule->offset + rule->secs; rule->computed_for = year;}/* Figure out the correct timezone for TM and set `__tzname', `__timezone', and `__daylight' accordingly. */voidinternal_function__tz_compute (timer, tm, use_localtime) time_t timer; struct tm *tm; int use_localtime;{ compute_change (&tz_rules[0], 1900 + tm->tm_year); compute_change (&tz_rules[1], 1900 + tm->tm_year); if (use_localtime) { int isdst; /* We have to distinguish between northern and southern hemisphere. For the latter the daylight saving time ends in the next year. */ if (__builtin_expect (tz_rules[0].change > tz_rules[1].change, 0)) isdst = (timer < tz_rules[1].change || timer >= tz_rules[0].change); else isdst = (timer >= tz_rules[0].change && timer < tz_rules[1].change); tm->tm_isdst = isdst; tm->tm_zone = __tzname[isdst]; tm->tm_gmtoff = tz_rules[isdst].offset; }}/* Reinterpret the TZ environment variable and set `tzname'. */#undef tzsetvoid__tzset (void){ __libc_lock_lock (tzset_lock); tzset_internal (1, 1); if (!__use_tzfile) { /* Set `tzname'. */ __tzname[0] = (char *) tz_rules[0].name; __tzname[1] = (char *) tz_rules[1].name; } __libc_lock_unlock (tzset_lock);}weak_alias (__tzset, tzset)/* Return the `struct tm' representation of *TIMER in the local timezone. Use local time if USE_LOCALTIME is nonzero, UTC otherwise. */struct tm *__tz_convert (const time_t *timer, int use_localtime, struct tm *tp){ long int leap_correction; int leap_extra_secs; if (timer == NULL) { __set_errno (EINVAL); return NULL; } __libc_lock_lock (tzset_lock); /* Update internal database according to current TZ setting. POSIX.1 8.3.7.2 says that localtime_r is not required to set tzname. This is a good idea since this allows at least a bit more parallelism. */ tzset_internal (tp == &_tmbuf && use_localtime, 1); if (__use_tzfile) __tzfile_compute (*timer, use_localtime, &leap_correction, &leap_extra_secs, tp); else { if (! __offtime (timer, 0, tp)) tp = NULL; else __tz_compute (*timer, tp, use_localtime); leap_correction = 0L; leap_extra_secs = 0; } if (tp) { if (! use_localtime) { tp->tm_isdst = 0; tp->tm_zone = "GMT"; tp->tm_gmtoff = 0L; } if (__offtime (timer, tp->tm_gmtoff - leap_correction, tp)) tp->tm_sec += leap_extra_secs; else tp = NULL; } __libc_lock_unlock (tzset_lock); return tp;}libc_freeres_fn (free_mem){ while (tzstring_list != NULL) { struct tzstring_l *old = tzstring_list; tzstring_list = tzstring_list->next; free (old); } free (old_tz); old_tz = NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -