home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_02 / 1102022a < prev    next >
Text File  |  1992-12-09  |  1KB  |  57 lines

  1. /* _Getzone function */
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "xtime.h"
  6.  
  7.         /* static data */
  8. static const char *defzone = ":UTC:UTC:0";
  9. static char *tzone = NULL;
  10.  
  11. static char *reformat(const char *s)
  12.     {    /* reformat TZ */
  13.     int i, val;
  14.     static char tzbuf[] = ":EST:EDT:+0300";
  15.  
  16.     for (i = 1; i <= 3; ++i)
  17.         if (isalpha(*s))
  18.             tzbuf[i] = *s, tzbuf[i + 4] = *s++;
  19.         else
  20.             return (NULL);
  21.     tzbuf[9] = *s == '-' || *s == '+' ? *s++ : '+';
  22.     if (!isdigit(*s))
  23.         return (NULL);
  24.     val = *s++ - '0';
  25.     if (isdigit(*s))
  26.         val = 10 * val + *s++ - '0';
  27.     for (val *= 60, i = 13; 10 <= i; --i, val /= 10)
  28.         tzbuf[i] = val % 10 + '0';
  29.     if (isalpha(*s))
  30.         for (i = 5; i <= 7; ++i)
  31.             if (isalpha(*s))
  32.                 tzbuf[i] = *s++;
  33.             else
  34.                 return (NULL);
  35.     return (*s == '\0' ? tzbuf : NULL);
  36.     }
  37.  
  38. const char *_Getzone(void)
  39.     {    /* get time zone information */
  40.     const char *s;
  41.  
  42.     if (tzone)
  43.         ;
  44.     else if ((s = getenv("TIMEZONE")) != NULL)
  45.         {    /* copy desired format */
  46.         if ((tzone = (char *)malloc(strlen(s) + 1))
  47.             != NULL)
  48.             strcpy(tzone, s);
  49.         }
  50.     else if ((s = getenv("TZ")) != NULL)
  51.         tzone = reformat(s);
  52.     if (tzone == NULL)
  53.         tzone = (char *)defzone;
  54.     return (tzone);
  55.     }
  56.  
  57.