home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / time / tzfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-16  |  7.5 KB  |  308 lines

  1. /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <time.h>
  23. #include <string.h>
  24. #include <limits.h>
  25.  
  26. #define    NOID
  27. #include <tzfile.h>
  28.  
  29. #ifndef    HAVE_GNU_LD
  30. #define    __tzname    tzname
  31. #define    __daylight    daylight
  32. #define    __timezone    timezone
  33. #endif
  34.  
  35. int __use_tzfile = 0;
  36.  
  37. struct ttinfo
  38.   {
  39.     long int offset;        /* Seconds east of GMT.  */
  40.     unsigned char isdst;    /* Used to set tm_isdst.  */
  41.     unsigned char idx;        /* Index into `zone_names'.  */
  42.     unsigned char isstd;    /* Transition times are standard time.  */
  43.   };
  44.  
  45. struct leap
  46.   {
  47.     time_t transition;        /* Time the transition takes effect.  */
  48.     long int change;        /* Seconds of correction to apply.  */
  49.   };
  50.  
  51. static size_t num_transitions;
  52. static time_t *transitions = NULL;
  53. static unsigned char *type_idxs = NULL;
  54. static size_t num_types;
  55. static struct ttinfo *types = NULL;
  56. static char *zone_names = NULL;
  57. static size_t num_leaps;
  58. static struct leap *leaps = NULL;
  59.  
  60. #define    uc2ul(x)    _uc2ul((unsigned char *) (x))
  61. #define    _uc2ul(x)                                  \
  62.   ((x)[3] + ((x)[2] << CHAR_BIT) + ((x)[1] << (2 * CHAR_BIT)) +              \
  63.    ((x)[0] << (3 * CHAR_BIT)))
  64.  
  65. void
  66. DEFUN(__tzfile_read, (file), CONST char *file)
  67. {
  68.   size_t num_isstd;
  69.   register FILE *f;
  70.   struct tzhead tzhead;
  71.   size_t chars;
  72.   register size_t i;
  73.  
  74.   __use_tzfile = 0;
  75.  
  76.   if (transitions != NULL)
  77.     free((PTR) transitions);
  78.   transitions = NULL;
  79.   if (type_idxs != NULL)
  80.     free((PTR) type_idxs);
  81.   type_idxs = NULL;
  82.   if (types != NULL)
  83.     free((PTR) types);
  84.   types = NULL;
  85.   if (zone_names != NULL)
  86.     free((PTR) zone_names);
  87.   zone_names = NULL;
  88.   if (leaps != NULL)
  89.     free((PTR) leaps);
  90.   leaps = NULL;
  91.  
  92.   if (file == NULL || *file == '\0')
  93.     file = TZDEFAULT;
  94.  
  95.   if (*file != '/')
  96.     {
  97.       static CONST char tzdir[] = TZDIR;
  98.       register CONST unsigned int len = strlen(file) + 1;
  99.       char *new = (char *) __alloca(sizeof(tzdir) + len);
  100.       memcpy(new, tzdir, sizeof(tzdir) - 1);
  101.       new[sizeof(tzdir) - 1] = '/';
  102.       memcpy(&new[sizeof(tzdir)], file, len);
  103.       file = new;
  104.     }
  105.  
  106.   f = fopen(file, "r");
  107.   if (f == NULL)
  108.     return;
  109.  
  110.   if (fread((PTR) &tzhead, sizeof(tzhead), 1, f) != 1)
  111.     goto lose;
  112.  
  113.   num_transitions = (size_t) uc2ul(tzhead.tzh_timecnt);
  114.   num_types = (size_t) uc2ul(tzhead.tzh_typecnt);
  115.   chars = (size_t) uc2ul(tzhead.tzh_charcnt);
  116.   num_leaps = (size_t) uc2ul(tzhead.tzh_leapcnt);
  117.   num_isstd = (size_t) uc2ul(tzhead.tzh_ttisstdcnt);
  118.  
  119.   if (num_transitions > 0)
  120.     {
  121.       transitions = (time_t *) malloc (num_transitions * sizeof(time_t));
  122.       if (transitions == NULL)
  123.     goto lose;
  124.       type_idxs = (unsigned char *) malloc (num_transitions);
  125.       if (type_idxs == NULL)
  126.     goto lose;
  127.     }
  128.   if (num_types > 0)
  129.     {
  130.       types = (struct ttinfo *) malloc (num_types * sizeof (struct ttinfo));
  131.       if (types == NULL)
  132.     goto lose;
  133.     }
  134.   if (chars > 0)
  135.     {
  136.       zone_names = (char *) malloc (chars);
  137.       if (zone_names == NULL)
  138.     goto lose;
  139.     }
  140.   if (num_leaps > 0)
  141.     {
  142.       leaps = (struct leap *) malloc (num_leaps * sizeof (struct leap));
  143.       if (leaps == NULL)
  144.     goto lose;
  145.     }
  146.  
  147.   if (fread((PTR) transitions, sizeof(time_t),
  148.         num_transitions, f) != num_transitions ||
  149.       fread((PTR) type_idxs, 1, num_transitions, f) != num_transitions)
  150.     goto lose;
  151.  
  152.   for (i = 0; i < num_transitions; ++i)
  153.     transitions[i] = uc2ul (&transitions[i]);
  154.  
  155.   for (i = 0; i < num_types; ++i)
  156.     {
  157.       unsigned char x[4];
  158.       if (fread((PTR) x, 1, 4, f) != 4 ||
  159.       fread((PTR) &types[i].isdst, 1, 1, f) != 1 ||
  160.       fread((PTR) &types[i].idx, 1, 1, f) != 1)
  161.     goto lose;
  162.       types[i].offset = (long int) uc2ul(x);
  163.     }
  164.  
  165.   if (fread((PTR) zone_names, 1, chars, f) != chars)
  166.     goto lose;
  167.  
  168.   for (i = 0; i < num_leaps; ++i)
  169.     {
  170.       unsigned char x[4];
  171.       if (fread((PTR) x, 1, sizeof(x), f) != sizeof(x))
  172.     goto lose;
  173.       leaps[i].transition = (time_t) uc2ul(x);
  174.       if (fread((PTR) x, 1, sizeof(x), f) != sizeof(x))
  175.     goto lose;
  176.       leaps[i].change = (long int) uc2ul(x);
  177.     }
  178.  
  179.   for (i = 0; i < num_isstd; ++i)
  180.     {
  181.       char c = getc(f);
  182.       if (c == EOF)
  183.     goto lose;
  184.       types[i].isstd = c != 0;
  185.     }
  186.   while (i < num_types)
  187.     types[i++].isstd = 0;
  188.  
  189.   (void) fclose(f);
  190.   __use_tzfile = 1;
  191.   return;
  192.  
  193.  lose:;
  194.   (void) fclose(f);
  195. }
  196.  
  197. void
  198. DEFUN(__tzfile_default, (std, dst, stdoff, dstoff),
  199.       char *std AND char *dst AND
  200.       long int stdoff AND long int dstoff)
  201. {
  202.   size_t stdlen, dstlen, i;
  203.  
  204.   __tzfile_read (TZDEFRULES);
  205.   if (!__use_tzfile)
  206.     return;
  207.  
  208.   if (num_types < 2)
  209.     {
  210.       __use_tzfile = 0;
  211.       return;
  212.     }
  213.  
  214.   free (zone_names);
  215.  
  216.   stdlen = strlen (std) + 1;
  217.   dstlen = strlen (dst) + 1;
  218.   zone_names = malloc (stdlen + dstlen);
  219.   if (zone_names == NULL)
  220.     {
  221.       __use_tzfile = 0;
  222.       return;
  223.     }
  224.   memcpy (zone_names, std, stdlen);
  225.   memcpy (&zone_names[stdlen], dst, dstlen);
  226.  
  227.   for (i = 0; i < num_types; ++i)
  228.     if (types[i].isdst)
  229.       {
  230.     types[i].idx = stdlen;
  231.     if (dst[0] != '\0')
  232.       types[i].offset = dstoff;
  233.       }
  234.     else
  235.       {
  236.     types[i].idx = 0;
  237.     if (dst[0] != '\0')
  238.       types[i].offset = stdoff;
  239.       }
  240. }
  241.  
  242. int
  243. DEFUN(__tzfile_compute, (timer, leap_correct, leap_hit),
  244.       time_t timer AND long int *leap_correct AND int *leap_hit)
  245. {
  246.   struct ttinfo *info;
  247.   register size_t i;
  248.  
  249.   if (num_transitions == 0 || timer < transitions[0])
  250.     {
  251.       /* TIMER is before any transition (or there are no transitions).
  252.      Choose the first non-DST type
  253.      (or the first if they're all DST types).  */
  254.       i = 0;
  255.       while (i < num_types && types[i].isdst)
  256.     ++i;
  257.       if (i == num_types)
  258.     i = 0;
  259.     }
  260.   else
  261.     {
  262.       /* Find the first transition after TIMER, and
  263.      then pick the type of the transition before it.  */
  264.       for (i = 1; i < num_transitions; ++i)
  265.     if (timer < transitions[i])
  266.       break;
  267.       i = type_idxs[i - 1];
  268.     }
  269.  
  270.   info = &types[i];
  271.   __daylight = info->isdst;
  272.   __timezone = info->offset;
  273.   for (i = 0; i < num_types && i < sizeof (__tzname) / sizeof (__tzname[0]);
  274.        ++i)
  275.     __tzname[types[i].isdst] = &zone_names[types[i].idx];
  276.   if (info->isdst < sizeof (__tzname) / sizeof (__tzname[0]))
  277.     __tzname[info->isdst] = &zone_names[info->idx];
  278.  
  279.   *leap_correct = 0L;
  280.   *leap_hit = 0;
  281.  
  282.   /* Find the last leap second correction transition time before TIMER.  */
  283.   i = num_leaps;
  284.   do
  285.     if (i-- == 0)
  286.       return 1;
  287.   while (timer < leaps[i].transition);
  288.  
  289.   /* Apply its correction.  */
  290.   *leap_correct = leaps[i].change;
  291.  
  292.   if (timer == leaps[i].transition && /* Exactly at the transition time.  */
  293.       ((i == 0 && leaps[i].change > 0) ||
  294.        leaps[i].change > leaps[i - 1].change))
  295.     {
  296.       *leap_hit = 1;
  297.       while (i > 0 &&
  298.          leaps[i].transition == leaps[i - 1].transition + 1 &&
  299.          leaps[i].change == leaps[i - 1].change + 1)
  300.     {
  301.       ++*leap_hit;
  302.       --i;
  303.     }
  304.     }
  305.  
  306.   return 1;
  307. }
  308.