Back to index

glibc  2.9
Defines | Functions | Variables
time.h File Reference
#include <time/time.h>
#include <xlocale.h>

Go to the source code of this file.

Defines

#define CLOCK_IDFIELD_SIZE   3

Functions

__BEGIN_DECLS __typeof (strftime_l) __strftime_l
 libc_hidden_proto (__strftime_l) extern __typeof(strptime_l) __strptime_l
 libc_hidden_proto (time) libc_hidden_proto(asctime) libc_hidden_proto(mktime) libc_hidden_proto(timelocal) libc_hidden_proto(localtime) libc_hidden_proto(strftime) libc_hidden_proto(strptime) librt_hidden_proto(clock_gettime) struct tm
char * __tzstring (const char *string)
void __tzfile_read (const char *file, size_t extra, char **extrap)
void __tzfile_compute (time_t timer, int use_localtime, long int *leap_correct, int *leap_hit, struct tm *tp)
void __tzfile_default (const char *std, const char *dst, long int stdoff, long int dstoff)
void __tzset_parse_tz (const char *tz)
void __tz_compute (time_t timer, struct tm *tm, int use_localtime) __THROW internal_function
time_t __mktime_internal (struct tm *__tp, struct tm *(*__func)(const time_t *, struct tm *), time_t *__offset)
struct tm__localtime_r (__const time_t *__timer, struct tm *__tp) attribute_hidden
struct tm__gmtime_r (__const time_t *__restrict __timer, struct tm *__restrict __tp)
 libc_hidden_proto (__gmtime_r) extern int __offtime(__const time_t *__timer
char * __asctime_r (__const struct tm *__tp, char *__buf)
void __tzset (void)
struct tm__tz_convert (const time_t *timer, int use_localtime, struct tm *tp)
long int __tzname_max (void)
int __nanosleep (__const struct timespec *__requested_time, struct timespec *__remaining)
 libc_hidden_proto (__nanosleep) extern int __nanosleep_nocancel(__const struct timespec *__requested_time
int __getdate_r (__const char *__string, struct tm *__resbufp)
int __getclktck (void)
char * __strptime_internal (const char *rp, const char *fmt, struct tm *tm, void *statep, __locale_t locparam) internal_function
double __difftime (time_t time1, time_t time0)

Variables

const unsigned short int
__mon_yday[2][13] 
attribute_hidden
long int __offset
long int struct tm__tp

Define Documentation

#define CLOCK_IDFIELD_SIZE   3

Definition at line 110 of file time.h.


Function Documentation

char* __asctime_r ( __const struct tm __tp,
char *  __buf 
)

Here is the caller graph for this function:

double __difftime ( time_t  time1,
time_t  time0 
)

Definition at line 103 of file difftime.c.

{
  /* Convert to double and then subtract if no double-rounding error could
     result.  */

  if (TYPE_BITS (time_t) <= DBL_MANT_DIG
      || (TYPE_FLOATING (time_t) && sizeof (time_t) < sizeof (long double)))
    return (double) time1 - (double) time0;

  /* Likewise for long double.  */

  if (TYPE_BITS (time_t) <= LDBL_MANT_DIG || TYPE_FLOATING (time_t))
    return (long double) time1 - (long double) time0;

  /* Subtract the smaller integer from the larger, convert the difference to
     double, and then negate if needed.  */

  return time1 < time0 ? - subtract (time0, time1) : subtract (time1, time0);
}

Here is the call graph for this function:

int __getclktck ( void  )

Definition at line 23 of file getclktck.c.

{
#ifdef CLK_TCK
  return CLK_TCK;
#else
  return 60;
#endif
}

Here is the caller graph for this function:

int __getdate_r ( __const char *  __string,
struct tm __resbufp 
)

Here is the caller graph for this function:

struct tm* __gmtime_r ( __const time_t *__restrict  __timer,
struct tm *__restrict  __tp 
) [read]
struct tm* __localtime_r ( __const time_t __timer,
struct tm __tp 
) [read]
time_t __mktime_internal ( struct tm __tp,
struct tm *(*)(const time_t *, struct tm *)  __func,
time_t __offset 
)

Definition at line 275 of file mktime.c.

{
  time_t t, gt, t0, t1, t2;
  struct tm tm;

  /* The maximum number of probes (calls to CONVERT) should be enough
     to handle any combinations of time zone rule changes, solar time,
     leap seconds, and oscillations around a spring-forward gap.
     POSIX.1 prohibits leap seconds, but some hosts have them anyway.  */
  int remaining_probes = 6;

  /* Time requested.  Copy it in case CONVERT modifies *TP; this can
     occur if TP is localtime's returned value and CONVERT is localtime.  */
  int sec = tp->tm_sec;
  int min = tp->tm_min;
  int hour = tp->tm_hour;
  int mday = tp->tm_mday;
  int mon = tp->tm_mon;
  int year_requested = tp->tm_year;
  /* Normalize the value.  */
  int isdst = ((tp->tm_isdst >> (8 * sizeof (tp->tm_isdst) - 1))
              | (tp->tm_isdst != 0));

  /* 1 if the previous probe was DST.  */
  int dst2;

  /* Ensure that mon is in range, and set year accordingly.  */
  int mon_remainder = mon % 12;
  int negative_mon_remainder = mon_remainder < 0;
  int mon_years = mon / 12 - negative_mon_remainder;
  long int lyear_requested = year_requested;
  long int year = lyear_requested + mon_years;

  /* The other values need not be in range:
     the remaining code handles minor overflows correctly,
     assuming int and time_t arithmetic wraps around.
     Major overflows are caught at the end.  */

  /* Calculate day of year from year, month, and day of month.
     The result need not be in range.  */
  int mon_yday = ((__mon_yday[leapyear (year)]
                 [mon_remainder + 12 * negative_mon_remainder])
                - 1);
  long int lmday = mday;
  long int yday = mon_yday + lmday;

  time_t guessed_offset = *offset;

  int sec_requested = sec;

  if (LEAP_SECONDS_POSSIBLE)
    {
      /* Handle out-of-range seconds specially,
        since ydhms_tm_diff assumes every minute has 60 seconds.  */
      if (sec < 0)
       sec = 0;
      if (59 < sec)
       sec = 59;
    }

  /* Invert CONVERT by probing.  First assume the same offset as last
     time.  */

  t0 = ydhms_diff (year, yday, hour, min, sec,
                 EPOCH_YEAR - TM_YEAR_BASE, 0, 0, 0, - guessed_offset);

  if (TIME_T_MAX / INT_MAX / 366 / 24 / 60 / 60 < 3)
    {
      /* time_t isn't large enough to rule out overflows, so check
        for major overflows.  A gross check suffices, since if t0
        has overflowed, it is off by a multiple of TIME_T_MAX -
        TIME_T_MIN + 1.  So ignore any component of the difference
        that is bounded by a small value.  */

      /* Approximate log base 2 of the number of time units per
        biennium.  A biennium is 2 years; use this unit instead of
        years to avoid integer overflow.  For example, 2 average
        Gregorian years are 2 * 365.2425 * 24 * 60 * 60 seconds,
        which is 63113904 seconds, and rint (log2 (63113904)) is
        26.  */
      int ALOG2_SECONDS_PER_BIENNIUM = 26;
      int ALOG2_MINUTES_PER_BIENNIUM = 20;
      int ALOG2_HOURS_PER_BIENNIUM = 14;
      int ALOG2_DAYS_PER_BIENNIUM = 10;
      int LOG2_YEARS_PER_BIENNIUM = 1;

      int approx_requested_biennia =
       (SHR (year_requested, LOG2_YEARS_PER_BIENNIUM)
        - SHR (EPOCH_YEAR - TM_YEAR_BASE, LOG2_YEARS_PER_BIENNIUM)
        + SHR (mday, ALOG2_DAYS_PER_BIENNIUM)
        + SHR (hour, ALOG2_HOURS_PER_BIENNIUM)
        + SHR (min, ALOG2_MINUTES_PER_BIENNIUM)
        + (LEAP_SECONDS_POSSIBLE
           ? 0
           : SHR (sec, ALOG2_SECONDS_PER_BIENNIUM)));

      int approx_biennia = SHR (t0, ALOG2_SECONDS_PER_BIENNIUM);
      int diff = approx_biennia - approx_requested_biennia;
      int abs_diff = diff < 0 ? - diff : diff;

      /* IRIX 4.0.5 cc miscalculates TIME_T_MIN / 3: it erroneously
        gives a positive value of 715827882.  Setting a variable
        first then doing math on it seems to work.
        (ghazi@caip.rutgers.edu) */
      time_t time_t_max = TIME_T_MAX;
      time_t time_t_min = TIME_T_MIN;
      time_t overflow_threshold =
       (time_t_max / 3 - time_t_min / 3) >> ALOG2_SECONDS_PER_BIENNIUM;

      if (overflow_threshold < abs_diff)
       {
         /* Overflow occurred.  Try repairing it; this might work if
            the time zone offset is enough to undo the overflow.  */
         time_t repaired_t0 = -1 - t0;
         approx_biennia = SHR (repaired_t0, ALOG2_SECONDS_PER_BIENNIUM);
         diff = approx_biennia - approx_requested_biennia;
         abs_diff = diff < 0 ? - diff : diff;
         if (overflow_threshold < abs_diff)
           return -1;
         guessed_offset += repaired_t0 - t0;
         t0 = repaired_t0;
       }
    }

  /* Repeatedly use the error to improve the guess.  */

  for (t = t1 = t2 = t0, dst2 = 0;
       (gt = guess_time_tm (year, yday, hour, min, sec, &t,
                         ranged_convert (convert, &t, &tm)),
       t != gt);
       t1 = t2, t2 = t, t = gt, dst2 = tm.tm_isdst != 0)
    if (t == t1 && t != t2
       && (tm.tm_isdst < 0
           || (isdst < 0
              ? dst2 <= (tm.tm_isdst != 0)
              : (isdst != 0) != (tm.tm_isdst != 0))))
      /* We can't possibly find a match, as we are oscillating
        between two values.  The requested time probably falls
        within a spring-forward gap of size GT - T.  Follow the common
        practice in this case, which is to return a time that is GT - T
        away from the requested time, preferring a time whose
        tm_isdst differs from the requested value.  (If no tm_isdst
        was requested and only one of the two values has a nonzero
        tm_isdst, prefer that value.)  In practice, this is more
        useful than returning -1.  */
      goto offset_found;
    else if (--remaining_probes == 0)
      return -1;

  /* We have a match.  Check whether tm.tm_isdst has the requested
     value, if any.  */
  if (isdst != tm.tm_isdst && 0 <= isdst && 0 <= tm.tm_isdst)
    {
      /* tm.tm_isdst has the wrong value.  Look for a neighboring
        time with the right value, and use its UTC offset.

        Heuristic: probe the adjacent timestamps in both directions,
        looking for the desired isdst.  This should work for all real
        time zone histories in the tz database.  */

      /* Distance between probes when looking for a DST boundary.  In
        tzdata2003a, the shortest period of DST is 601200 seconds
        (e.g., America/Recife starting 2000-10-08 01:00), and the
        shortest period of non-DST surrounded by DST is 694800
        seconds (Africa/Tunis starting 1943-04-17 01:00).  Use the
        minimum of these two values, so we don't miss these short
        periods when probing.  */
      int stride = 601200;

      /* The longest period of DST in tzdata2003a is 536454000 seconds
        (e.g., America/Jujuy starting 1946-10-01 01:00).  The longest
        period of non-DST is much longer, but it makes no real sense
        to search for more than a year of non-DST, so use the DST
        max.  */
      int duration_max = 536454000;

      /* Search in both directions, so the maximum distance is half
        the duration; add the stride to avoid off-by-1 problems.  */
      int delta_bound = duration_max / 2 + stride;

      int delta, direction;

      for (delta = stride; delta < delta_bound; delta += stride)
       for (direction = -1; direction <= 1; direction += 2)
         {
           time_t ot = t + delta * direction;
           if ((ot < t) == (direction < 0))
             {
              struct tm otm;
              ranged_convert (convert, &ot, &otm);
              if (otm.tm_isdst == isdst)
                {
                  /* We found the desired tm_isdst.
                     Extrapolate back to the desired time.  */
                  t = guess_time_tm (year, yday, hour, min, sec, &ot, &otm);
                  ranged_convert (convert, &t, &tm);
                  goto offset_found;
                }
             }
         }
    }

 offset_found:
  *offset = guessed_offset + t - t0;

  if (LEAP_SECONDS_POSSIBLE && sec_requested != tm.tm_sec)
    {
      /* Adjust time to reflect the tm_sec requested, not the normalized value.
        Also, repair any damage from a false match due to a leap second.  */
      int sec_adjustment = (sec == 0 && tm.tm_sec == 60) - sec;
      t1 = t + sec_requested;
      t2 = t1 + sec_adjustment;
      if (((t1 < t) != (sec_requested < 0))
         | ((t2 < t1) != (sec_adjustment < 0))
         | ! convert (&t2, &tm))
       return -1;
      t = t2;
    }

  *tp = tm;
  return t;
}

Here is the call graph for this function:

int __nanosleep ( __const struct timespec __requested_time,
struct timespec __remaining 
)

Here is the caller graph for this function:

char* __strptime_internal ( const char *  rp,
const char *  fmt,
struct tm tm,
void *  statep,
__locale_t  locparam 
)

Here is the caller graph for this function:

__BEGIN_DECLS __typeof ( strftime_l  )
void __tz_compute ( time_t  timer,
struct tm tm,
int  use_localtime 
)

Definition at line 541 of file tzset.c.

{
  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;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

struct tm* __tz_convert ( const time_t timer,
int  use_localtime,
struct tm tp 
) [read]

Here is the caller graph for this function:

void __tzfile_compute ( time_t  timer,
int  use_localtime,
long int leap_correct,
int leap_hit,
struct tm tp 
)

Definition at line 591 of file tzfile.c.

{
  register size_t i;

  if (use_localtime)
    {
      __tzname[0] = NULL;
      __tzname[1] = NULL;

      if (__builtin_expect (num_transitions == 0 || timer < transitions[0], 0))
       {
         /* TIMER is before any transition (or there are no transitions).
            Choose the first non-DST type
            (or the first if they're all DST types).  */
         i = 0;
         while (i < num_types && types[i].isdst)
           {
             if (__tzname[1] == NULL)
              __tzname[1] = __tzstring (&zone_names[types[i].idx]);

             ++i;
           }

         if (i == num_types)
           i = 0;
         __tzname[0] = __tzstring (&zone_names[types[i].idx]);
         if (__tzname[1] == NULL)
           {
             size_t j = i;
             while (j < num_types)
              if (types[j].isdst)
                {
                  __tzname[1] = __tzstring (&zone_names[types[j].idx]);
                  break;
                }
              else
                ++j;
           }
       }
      else if (__builtin_expect (timer >= transitions[num_transitions - 1], 0))
       {
         if (__builtin_expect (tzspec == NULL, 0))
           {
           use_last:
             i = num_transitions;
             goto found;
           }

         /* Parse the POSIX TZ-style string.  */
         __tzset_parse_tz (tzspec);

         /* Convert to broken down structure.  If this fails do not
            use the string.  */
         if (__builtin_expect (! __offtime (&timer, 0, tp), 0))
           goto use_last;

         /* Use the rules from the TZ string to compute the change.  */
         __tz_compute (timer, tp, 1);

         /* If tzspec comes from posixrules loaded by __tzfile_default,
            override the STD and DST zone names with the ones user
            requested in TZ envvar.  */
         if (__builtin_expect (zone_names == (char *) &leaps[num_leaps], 0))
           {
             assert (num_types == 2);
             __tzname[0] = __tzstring (zone_names);
             __tzname[1] = __tzstring (&zone_names[strlen (zone_names) + 1]);
           }

         *leap_correct = 0L;
         *leap_hit = 0;
         return;
       }
      else
       {
         /* Find the first transition after TIMER, and
            then pick the type of the transition before it.  */
         size_t lo = 0;
         size_t hi = num_transitions - 1;
         /* Assume that DST is changing twice a year and guess initial
            search spot from it.
            Half of a gregorian year has on average 365.2425 * 86400 / 2
            = 15778476 seconds.  */
         i = (transitions[num_transitions - 1] - timer) / 15778476;
         if (i < num_transitions)
           {
             i = num_transitions - 1 - i;
             if (timer < transitions[i])
              {
                if (i < 10 || timer >= transitions[i - 10])
                  {
                    /* Linear search.  */
                    while (timer < transitions[i - 1])
                     --i;
                    goto found;
                  }
                hi = i - 10;
              }
             else
              {
                if (i + 10 >= num_transitions || timer < transitions[i + 10])
                  {
                    /* Linear search.  */
                    while (timer >= transitions[i])
                     ++i;
                    goto found;
                  }
                lo = i + 10;
              }
           }

         /* Binary search.  */
         /* assert (timer >= transitions[lo] && timer < transitions[hi]); */
         while (lo + 1 < hi)
           {
             i = (lo + hi) / 2;
             if (timer < transitions[i])
              hi = i;
             else
              lo = i;
           }
         i = hi;

       found:
         /* assert (timer >= transitions[i - 1]
            && (i == num_transitions || timer < transitions[i])); */
         __tzname[types[type_idxs[i - 1]].isdst]
           = __tzstring (&zone_names[types[type_idxs[i - 1]].idx]);
         size_t j = i;
         while (j < num_transitions)
           {
             int type = type_idxs[j];
             int dst = types[type].isdst;
             int idx = types[type].idx;

             if (__tzname[dst] == NULL)
              {
                __tzname[dst] = __tzstring (&zone_names[idx]);

                if (__tzname[1 - dst] != NULL)
                  break;
              }

             ++j;
           }

         if (__builtin_expect (__tzname[0] == NULL, 0))
           __tzname[0] = __tzname[1];

         i = type_idxs[i - 1];
       }

      struct ttinfo *info = &types[i];
      __daylight = rule_stdoff != rule_dstoff;
      __timezone = -rule_stdoff;

      if (__tzname[0] == NULL)
       {
         /* This should only happen if there are no transition rules.
            In this case there should be only one single type.  */
         assert (num_types == 1);
         __tzname[0] = __tzstring (zone_names);
       }
      if (__tzname[1] == NULL)
       /* There is no daylight saving time.  */
       __tzname[1] = __tzname[0];
      tp->tm_isdst = info->isdst;
      assert (strcmp (&zone_names[info->idx], __tzname[tp->tm_isdst]) == 0);
      tp->tm_zone = __tzname[tp->tm_isdst];
      tp->tm_gmtoff = info->offset;
    }

  *leap_correct = 0L;
  *leap_hit = 0;

  /* Find the last leap second correction transition time before TIMER.  */
  i = num_leaps;
  do
    if (i-- == 0)
      return;
  while (timer < leaps[i].transition);

  /* Apply its correction.  */
  *leap_correct = leaps[i].change;

  if (timer == leaps[i].transition && /* Exactly at the transition time.  */
      ((i == 0 && leaps[i].change > 0) ||
       leaps[i].change > leaps[i - 1].change))
    {
      *leap_hit = 1;
      while (i > 0
            && leaps[i].transition == leaps[i - 1].transition + 1
            && leaps[i].change == leaps[i - 1].change + 1)
       {
         ++*leap_hit;
         --i;
       }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void __tzfile_default ( const char *  std,
const char *  dst,
long int  stdoff,
long int  dstoff 
)

Definition at line 507 of file tzfile.c.

{
  size_t stdlen = strlen (std) + 1;
  size_t dstlen = strlen (dst) + 1;
  size_t i;
  int isdst;
  char *cp;

  __tzfile_read (TZDEFRULES, stdlen + dstlen, &cp);
  if (!__use_tzfile)
    return;

  if (num_types < 2)
    {
      __use_tzfile = 0;
      return;
    }

  /* Ignore the zone names read from the file and use the given ones
     instead.  */
  __mempcpy (__mempcpy (cp, std, stdlen), dst, dstlen);
  zone_names = cp;

  /* Now there are only two zones, regardless of what the file contained.  */
  num_types = 2;

  /* Now correct the transition times for the user-specified standard and
     daylight offsets from GMT.  */
  isdst = 0;
  for (i = 0; i < num_transitions; ++i)
    {
      struct ttinfo *trans_type = &types[type_idxs[i]];

      /* We will use only types 0 (standard) and 1 (daylight).
        Fix up this transition to point to whichever matches
        the flavor of its original type.  */
      type_idxs[i] = trans_type->isdst;

      if (trans_type->isgmt)
       /* The transition time is in GMT.  No correction to apply.  */ ;
      else if (isdst && !trans_type->isstd)
       /* The type says this transition is in "local wall clock time", and
          wall clock time as of the previous transition was DST.  Correct
          for the difference between the rule's DST offset and the user's
          DST offset.  */
       transitions[i] += dstoff - rule_dstoff;
      else
       /* This transition is in "local wall clock time", and wall clock
          time as of this iteration is non-DST.  Correct for the
          difference between the rule's standard offset and the user's
          standard offset.  */
       transitions[i] += stdoff - rule_stdoff;

      /* The DST state of "local wall clock time" for the next iteration is
        as specified by this transition.  */
      isdst = trans_type->isdst;
    }

  /* Now that we adjusted the transitions to the requested offsets,
     reset the rule_stdoff and rule_dstoff values appropriately.  They
     are used elsewhere.  */
  rule_stdoff = stdoff;
  rule_dstoff = dstoff;

  /* Reset types 0 and 1 to describe the user's settings.  */
  types[0].idx = 0;
  types[0].offset = stdoff;
  types[0].isdst = 0;
  types[1].idx = stdlen;
  types[1].offset = dstoff;
  types[1].isdst = 1;

  /* Reset the zone names to point to the user's names.  */
  __tzname[0] = (char *) std;
  __tzname[1] = (char *) dst;

  /* Set the timezone.  */
  __timezone = -types[0].offset;

  compute_tzname_max (stdlen + dstlen);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void __tzfile_read ( const char *  file,
size_t  extra,
char **  extrap 
)

Definition at line 104 of file tzfile.c.

{
  static const char default_tzdir[] = TZDIR;
  size_t num_isstd, num_isgmt;
  register FILE *f;
  struct tzhead tzhead;
  size_t chars;
  register size_t i;
  size_t total_size;
  size_t types_idx;
  size_t leaps_idx;
  int was_using_tzfile = __use_tzfile;
  int trans_width = 4;
  size_t tzspec_len;

  if (sizeof (time_t) != 4 && sizeof (time_t) != 8)
    abort ();

  __use_tzfile = 0;

  if (file == NULL)
    /* No user specification; use the site-wide default.  */
    file = TZDEFAULT;
  else if (*file == '\0')
    /* User specified the empty string; use UTC with no leap seconds.  */
    goto ret_free_transitions;
  else
    {
      /* We must not allow to read an arbitrary file in a setuid
        program.  So we fail for any file which is not in the
        directory hierachy starting at TZDIR
        and which is not the system wide default TZDEFAULT.  */
      if (__libc_enable_secure
         && ((*file == '/'
              && memcmp (file, TZDEFAULT, sizeof TZDEFAULT)
              && memcmp (file, default_tzdir, sizeof (default_tzdir) - 1))
             || strstr (file, "../") != NULL))
       /* This test is certainly a bit too restrictive but it should
          catch all critical cases.  */
       goto ret_free_transitions;
    }

  if (*file != '/')
    {
      const char *tzdir;
      unsigned int len, tzdir_len;
      char *new, *tmp;

      tzdir = getenv ("TZDIR");
      if (tzdir == NULL || *tzdir == '\0')
       {
         tzdir = default_tzdir;
         tzdir_len = sizeof (default_tzdir) - 1;
       }
      else
       tzdir_len = strlen (tzdir);
      len = strlen (file) + 1;
      new = (char *) __alloca (tzdir_len + 1 + len);
      tmp = __mempcpy (new, tzdir, tzdir_len);
      *tmp++ = '/';
      memcpy (tmp, file, len);
      file = new;
    }

  /* If we were already using tzfile, check whether the file changed.  */
  struct stat64 st;
  if (was_using_tzfile
      && stat64 (file, &st) == 0
      && tzfile_ino == st.st_ino && tzfile_dev == st.st_dev
      && tzfile_mtime == st.st_mtime)
    {
      /* Nothing to do.  */
      __use_tzfile = 1;
      return;
    }

  /* Note the file is opened with cancellation in the I/O functions
     disabled.  */
  f = fopen (file, "rc");
  if (f == NULL)
    goto ret_free_transitions;

  /* Get information about the file we are actually using.  */
  if (fstat64 (fileno (f), &st) != 0)
    {
      fclose (f);
      goto ret_free_transitions;
    }

  free ((void *) transitions);
  transitions = NULL;

  /* Remember the inode and device number and modification time.  */
  tzfile_dev = st.st_dev;
  tzfile_ino = st.st_ino;
  tzfile_mtime = st.st_mtime;

  /* No threads reading this stream.  */
  __fsetlocking (f, FSETLOCKING_BYCALLER);

 read_again:
  if (__builtin_expect (fread_unlocked ((void *) &tzhead, sizeof (tzhead),
                                   1, f) != 1, 0)
      || memcmp (tzhead.tzh_magic, TZ_MAGIC, sizeof (tzhead.tzh_magic)) != 0)
    goto lose;

  num_transitions = (size_t) decode (tzhead.tzh_timecnt);
  num_types = (size_t) decode (tzhead.tzh_typecnt);
  chars = (size_t) decode (tzhead.tzh_charcnt);
  num_leaps = (size_t) decode (tzhead.tzh_leapcnt);
  num_isstd = (size_t) decode (tzhead.tzh_ttisstdcnt);
  num_isgmt = (size_t) decode (tzhead.tzh_ttisgmtcnt);

  /* For platforms with 64-bit time_t we use the new format if available.  */
  if (sizeof (time_t) == 8 && trans_width == 4
      && tzhead.tzh_version[0] != '\0')
    {
      /* We use the 8-byte format.  */
      trans_width = 8;

      /* Position the stream before the second header.  */
      size_t to_skip = (num_transitions * (4 + 1)
                     + num_types * 6
                     + chars
                     + num_leaps * 8
                     + num_isstd
                     + num_isgmt);
      if (fseek (f, to_skip, SEEK_CUR) != 0)
       goto lose;

      goto read_again;
    }

  total_size = num_transitions * (sizeof (time_t) + 1);
  total_size = ((total_size + __alignof__ (struct ttinfo) - 1)
              & ~(__alignof__ (struct ttinfo) - 1));
  types_idx = total_size;
  total_size += num_types * sizeof (struct ttinfo) + chars;
  total_size = ((total_size + __alignof__ (struct leap) - 1)
              & ~(__alignof__ (struct leap) - 1));
  leaps_idx = total_size;
  total_size += num_leaps * sizeof (struct leap);
  tzspec_len = (sizeof (time_t) == 8 && trans_width == 8
              ? st.st_size - (ftello (f)
                            + num_transitions * (8 + 1)
                            + num_types * 6
                            + chars
                            + num_leaps * 8
                            + num_isstd
                            + num_isgmt) - 1 : 0);

  /* Allocate enough memory including the extra block requested by the
     caller.  */
  transitions = (time_t *) malloc (total_size + tzspec_len + extra);
  if (transitions == NULL)
    goto lose;

  type_idxs = (unsigned char *) transitions + (num_transitions
                                          * sizeof (time_t));
  types = (struct ttinfo *) ((char *) transitions + types_idx);
  zone_names = (char *) types + num_types * sizeof (struct ttinfo);
  leaps = (struct leap *) ((char *) transitions + leaps_idx);
  if (sizeof (time_t) == 8 && trans_width == 8)
    tzspec = (char *) leaps + num_leaps * sizeof (struct leap) + extra;
  else
    tzspec = NULL;
  if (extra > 0)
    *extrap = (char *) &leaps[num_leaps];

  if (sizeof (time_t) == 4 || __builtin_expect (trans_width == 8, 1))
    {
      if (__builtin_expect (fread_unlocked (transitions, trans_width + 1,
                                       num_transitions, f)
                         != num_transitions, 0))
       goto lose;
    }
  else
    {
      if (__builtin_expect (fread_unlocked (transitions, 4, num_transitions, f)
                         != num_transitions, 0)
         || __builtin_expect (fread_unlocked (type_idxs, 1, num_transitions,
                                          f) != num_transitions, 0))
       goto lose;
    }

  /* Check for bogus indices in the data file, so we can hereafter
     safely use type_idxs[T] as indices into `types' and never crash.  */
  for (i = 0; i < num_transitions; ++i)
    if (__builtin_expect (type_idxs[i] >= num_types, 0))
      goto lose;

  if ((BYTE_ORDER != BIG_ENDIAN && (sizeof (time_t) == 4 || trans_width == 4))
      || (BYTE_ORDER == BIG_ENDIAN && sizeof (time_t) == 8
         && trans_width == 4))
    {
      /* Decode the transition times, stored as 4-byte integers in
        network (big-endian) byte order.  We work from the end of
        the array so as not to clobber the next element to be
        processed when sizeof (time_t) > 4.  */
      i = num_transitions;
      while (i-- > 0)
       transitions[i] = decode ((char *) transitions + i * 4);
    }
  else if (BYTE_ORDER != BIG_ENDIAN && sizeof (time_t) == 8)
    {
      /* Decode the transition times, stored as 8-byte integers in
        network (big-endian) byte order.  */
      for (i = 0; i < num_transitions; ++i)
       transitions[i] = decode64 ((char *) transitions + i * 8);
    }

  for (i = 0; i < num_types; ++i)
    {
      unsigned char x[4];
      int c;
      if (__builtin_expect (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x),
                         0))
       goto lose;
      c = getc_unlocked (f);
      if (__builtin_expect ((unsigned int) c > 1u, 0))
       goto lose;
      types[i].isdst = c;
      c = getc_unlocked (f);
      if (__builtin_expect ((size_t) c > chars, 0))
       /* Bogus index in data file.  */
       goto lose;
      types[i].idx = c;
      types[i].offset = (long int) decode (x);
    }

  if (__builtin_expect (fread_unlocked (zone_names, 1, chars, f) != chars, 0))
    goto lose;

  for (i = 0; i < num_leaps; ++i)
    {
      unsigned char x[8];
      if (__builtin_expect (fread_unlocked (x, 1, trans_width, f)
                         != trans_width, 0))
       goto lose;
      if (sizeof (time_t) == 4 || trans_width == 4)
       leaps[i].transition = (time_t) decode (x);
      else
       leaps[i].transition = (time_t) decode64 (x);

      if (__builtin_expect (fread_unlocked (x, 1, 4, f) != 4, 0))
       goto lose;
      leaps[i].change = (long int) decode (x);
    }

  for (i = 0; i < num_isstd; ++i)
    {
      int c = getc_unlocked (f);
      if (__builtin_expect (c == EOF, 0))
       goto lose;
      types[i].isstd = c != 0;
    }
  while (i < num_types)
    types[i++].isstd = 0;

  for (i = 0; i < num_isgmt; ++i)
    {
      int c = getc_unlocked (f);
      if (__builtin_expect (c == EOF, 0))
       goto lose;
      types[i].isgmt = c != 0;
    }
  while (i < num_types)
    types[i++].isgmt = 0;

  /* Read the POSIX TZ-style information if possible.  */
  if (sizeof (time_t) == 8 && tzspec != NULL)
    {
      /* Skip over the newline first.  */
      if (getc_unlocked (f) != '\n'
         || (fread_unlocked (tzspec, 1, tzspec_len - 1, f)
             != tzspec_len - 1))
       tzspec = NULL;
      else
       tzspec[tzspec_len - 1] = '\0';
    }
  else if (sizeof (time_t) == 4 && tzhead.tzh_version[0] != '\0')
    {
      /* Get the TZ string.  */
      if (__builtin_expect (fread_unlocked ((void *) &tzhead, sizeof (tzhead),
                                       1, f) != 1, 0)
         || (memcmp (tzhead.tzh_magic, TZ_MAGIC, sizeof (tzhead.tzh_magic))
             != 0))
       goto lose;

      size_t num_transitions2 = (size_t) decode (tzhead.tzh_timecnt);
      size_t num_types2 = (size_t) decode (tzhead.tzh_typecnt);
      size_t chars2 = (size_t) decode (tzhead.tzh_charcnt);
      size_t num_leaps2 = (size_t) decode (tzhead.tzh_leapcnt);
      size_t num_isstd2 = (size_t) decode (tzhead.tzh_ttisstdcnt);
      size_t num_isgmt2 = (size_t) decode (tzhead.tzh_ttisgmtcnt);

      /* Position the stream before the second header.  */
      size_t to_skip = (num_transitions2 * (8 + 1)
                     + num_types2 * 6
                     + chars2
                     + num_leaps2 * 12
                     + num_isstd2
                     + num_isgmt2);
      off_t off;
      if (fseek (f, to_skip, SEEK_CUR) != 0
         || (off = ftello (f)) < 0
         || st.st_size < off + 2)
       goto lose;

      tzspec_len = st.st_size - off - 1;
      char *tzstr = alloca (tzspec_len);
      if (getc_unlocked (f) != '\n'
         || (fread_unlocked (tzstr, 1, tzspec_len - 1, f) != tzspec_len - 1))
       goto lose;
      tzstr[tzspec_len - 1] = '\0';
      tzspec = __tzstring (tzstr);
    }

  fclose (f);

  /* First "register" all timezone names.  */
  for (i = 0; i < num_types; ++i)
    (void) __tzstring (&zone_names[types[i].idx]);

  /* Find the standard and daylight time offsets used by the rule file.
     We choose the offsets in the types of each flavor that are
     transitioned to earliest in time.  */
  __tzname[0] = NULL;
  __tzname[1] = NULL;
  for (i = num_transitions; i > 0; )
    {
      int type = type_idxs[--i];
      int dst = types[type].isdst;

      if (__tzname[dst] == NULL)
       {
         int idx = types[type].idx;

         __tzname[dst] = __tzstring (&zone_names[idx]);

         if (__tzname[1 - dst] != NULL)
           break;
       }
    }
  if (__tzname[0] == NULL)
    {
      /* This should only happen if there are no transition rules.
        In this case there should be only one single type.  */
      assert (num_types == 1);
      __tzname[0] = __tzstring (zone_names);
    }
  if (__tzname[1] == NULL)
    __tzname[1] = __tzname[0];

  compute_tzname_max (chars);

  if (num_transitions == 0)
    /* Use the first rule (which should also be the only one).  */
    rule_stdoff = rule_dstoff = types[0].offset;
  else
    {
      int stdoff_set = 0, dstoff_set = 0;
      rule_stdoff = rule_dstoff = 0;
      i = num_transitions - 1;
      do
       {
         if (!stdoff_set && !types[type_idxs[i]].isdst)
           {
             stdoff_set = 1;
             rule_stdoff = types[type_idxs[i]].offset;
           }
         else if (!dstoff_set && types[type_idxs[i]].isdst)
           {
             dstoff_set = 1;
             rule_dstoff = types[type_idxs[i]].offset;
           }
         if (stdoff_set && dstoff_set)
           break;
       }
      while (i-- > 0);

      if (!dstoff_set)
       rule_dstoff = rule_stdoff;
    }

  __daylight = rule_stdoff != rule_dstoff;
  __timezone = -rule_stdoff;

  __use_tzfile = 1;
  return;

 lose:
  fclose (f);
 ret_free_transitions:
  free ((void *) transitions);
  transitions = NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

long int __tzname_max ( void  )

Definition at line 129 of file tzset.c.

{
  __libc_lock_lock (tzset_lock);

  tzset_internal (0, 0);

  __libc_lock_unlock (tzset_lock);

  return __tzname_cur_max;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void __tzset ( void  )

Definition at line 573 of file tzset.c.

{
  __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);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void __tzset_parse_tz ( const char *  tz)

Here is the caller graph for this function:

char* __tzstring ( const char *  string)

Definition at line 90 of file tzset.c.

{
  char *p;
  struct tzstring_l *t, *u, *new;
  size_t len = strlen (s);

  /* Walk the list and look for a match.  If this string is the same
     as the end of an already-allocated string, it can share space. */
  for (u = t = tzstring_list; t; u = t, t = t->next)
    if (len <= t->len)
      {
       p = &t->data[t->len - len];
       if (strcmp (s, p) == 0)
         return p;
      }

  /* Not found; allocate a new buffer. */
  new = malloc (sizeof (struct tzstring_l) + len + 1);
  if (!new)
    return NULL;

  new->next = NULL;
  new->len = len;
  strcpy (new->data, s);

  if (u)
    u->next = new;
  else
    tzstring_list = new;

  return new->data;
}

Here is the call graph for this function:

Here is the caller graph for this function:

libc_hidden_proto ( __strftime_l  )

Variable Documentation

Definition at line 72 of file time.h.

long int struct tm* __tp

Definition at line 73 of file time.h.

struct timespec* __remaining attribute_hidden

Definition at line 37 of file time.h.