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

  1. #ifndef lint
  2. #ifndef NOID
  3. static char    elsieid[] = "@(#)zdump.c    7.3";
  4. #endif /* !defined NOID */
  5. #endif /* !defined lint */
  6.  
  7. /*
  8. ** This code has been made independent of the rest of the time
  9. ** conversion package to increase confidence in the verification it provides.
  10. ** You can use this code to help in verifying other implementations.
  11. */
  12.  
  13. #include "stdio.h"    /* for stdout, stderr */
  14. #include "string.h"    /* for strcpy */
  15. #include "sys/types.h"    /* for time_t */
  16. #include "time.h"    /* for struct tm */
  17.  
  18. #ifndef MAX_STRING_LENGTH
  19. #define MAX_STRING_LENGTH    1024
  20. #endif /* !defined MAX_STRING_LENGTH */
  21.  
  22. #ifndef TRUE
  23. #define TRUE        1
  24. #endif /* !defined TRUE */
  25.  
  26. #ifndef FALSE
  27. #define FALSE        0
  28. #endif /* !defined FALSE */
  29.  
  30. #ifndef EXIT_SUCCESS
  31. #define EXIT_SUCCESS    0
  32. #endif /* !defined EXIT_SUCCESS */
  33.  
  34. #ifndef EXIT_FAILURE
  35. #define EXIT_FAILURE    1
  36. #endif /* !defined EXIT_FAILURE */
  37.  
  38. #ifndef SECSPERMIN
  39. #define SECSPERMIN    60
  40. #endif /* !defined SECSPERMIN */
  41.  
  42. #ifndef SECSPERHOUR
  43. #define SECSPERHOUR    3600
  44. #endif /* !defined SECSPERHOUR */
  45.  
  46. #ifndef HOURSPERDAY
  47. #define HOURSPERDAY    24
  48. #endif /* !defined HOURSPERDAY */
  49.  
  50. #ifndef EPOCH_YEAR
  51. #define EPOCH_YEAR    1970
  52. #endif /* !defined EPOCH_YEAR */
  53.  
  54. #ifndef DAYSPERNYEAR
  55. #define DAYSPERNYEAR    365
  56. #endif /* !defined DAYSPERNYEAR */
  57.  
  58. extern char **    environ;
  59. extern int    getopt();
  60. extern char *    optarg;
  61. extern int    optind;
  62. extern time_t    time();
  63. extern char *    tzname[2];
  64. extern void    tzset();
  65.  
  66. #ifdef USG
  67. extern void    exit();
  68. extern void    perror();
  69. #endif /* defined USG */
  70.  
  71. static char *    abbr();
  72. static long    delta();
  73. static void    hunt();
  74. static int    longest;
  75. static char *    progname;
  76. static void    show();
  77.  
  78. int
  79. main(argc, argv)
  80. int    argc;
  81. char *    argv[];
  82. {
  83.     register int    i, c;
  84.     register int    vflag;
  85.     register char *    cutoff;
  86.     register int    cutyear;
  87.     register long    cuttime;
  88.     time_t        now;
  89.     time_t        t, newt;
  90.     time_t        hibit;
  91.     struct tm    tm, newtm;
  92.  
  93.     progname = argv[0];
  94.     vflag = 0;
  95.     cutoff = NULL;
  96.     while ((c = getopt(argc, argv, "c:v")) == 'c' || c == 'v')
  97.         if (c == 'v')
  98.             vflag = 1;
  99.         else    cutoff = optarg;
  100.     if (c != EOF ||
  101.        (optind == argc - 1 && strcmp(argv[optind], "=") == 0)) {
  102.         (void) fprintf(stderr,
  103.             "%s: usage is %s [ -v ] [ -c cutoff ] zonename ...\n",
  104.             argv[0], argv[0]);
  105.         (void) exit(EXIT_FAILURE);
  106.     }
  107.     if (cutoff != NULL)
  108.         cutyear = atoi(cutoff);
  109.     /*
  110.     ** VERY approximate.
  111.     */
  112.     cuttime = (long) (cutyear - EPOCH_YEAR) *
  113.         SECSPERHOUR * HOURSPERDAY * DAYSPERNYEAR;
  114.     (void) time(&now);
  115.     longest = 0;
  116.     for (i = optind; i < argc; ++i)
  117.         if (strlen(argv[i]) > longest)
  118.             longest = strlen(argv[i]);
  119.     for (hibit = 1; (hibit << 1) != 0; hibit <<= 1)
  120.         ;
  121.     for (i = optind; i < argc; ++i) {
  122.         register char **    saveenv;
  123.         static char        buf[MAX_STRING_LENGTH];
  124.         char *            fakeenv[2];
  125.  
  126.         if (strlen(argv[i]) + 4 > sizeof buf) {
  127.             (void) fflush(stdout);
  128.             (void) fprintf(stderr, "%s: argument too long -- %s\n",
  129.                 progname, argv[i]);
  130.             (void) exit(EXIT_FAILURE);
  131.         }
  132.         (void) strcpy(buf, "TZ=");
  133.         (void) strcat(buf, argv[i]);
  134.         fakeenv[0] = buf;
  135.         fakeenv[1] = NULL;
  136.         saveenv = environ;
  137.         environ = fakeenv;
  138.         (void) tzset();
  139.         environ = saveenv;
  140.         show(argv[i], now, FALSE);
  141.         if (!vflag)
  142.             continue;
  143.         /*
  144.         ** Get lowest value of t.
  145.         */
  146.         t = hibit;
  147.         if (t > 0)        /* time_t is unsigned */
  148.             t = 0;
  149.         show(argv[i], t, TRUE);
  150.         t += SECSPERHOUR * HOURSPERDAY;
  151.         show(argv[i], t, TRUE);
  152.         tm = *localtime(&t);
  153.         (void) strncpy(buf, abbr(&tm), (sizeof buf) - 1);
  154.         for ( ; ; ) {
  155.             if (cutoff != NULL && t >= cuttime)
  156.                 break;
  157.             newt = t + SECSPERHOUR * 12;
  158.             if (cutoff != NULL && newt >= cuttime)
  159.                 break;
  160.             if (newt <= t)
  161.                 break;
  162.             newtm = *localtime(&newt);
  163.             if (delta(&newtm, &tm) != (newt - t) ||
  164.                 newtm.tm_isdst != tm.tm_isdst ||
  165.                 strcmp(abbr(&newtm), buf) != 0) {
  166.                     hunt(argv[i], t, newt);
  167.                     (void) strncpy(buf, abbr(&newtm),
  168.                         (sizeof buf) - 1);
  169.             }
  170.             t = newt;
  171.             tm = newtm;
  172.         }
  173.         /*
  174.         ** Get highest value of t.
  175.         */
  176.         t = ~((time_t) 0);
  177.         if (t < 0)        /* time_t is signed */
  178.             t &= ~hibit;
  179.         t -= SECSPERHOUR * HOURSPERDAY;
  180.         show(argv[i], t, TRUE);
  181.         t += SECSPERHOUR * HOURSPERDAY;
  182.         show(argv[i], t, TRUE);
  183.     }
  184.     if (fflush(stdout) || ferror(stdout)) {
  185.         (void) fprintf(stderr, "%s: Error writing standard output ",
  186.             argv[0]);
  187.         (void) perror("standard output");
  188.         (void) exit(EXIT_FAILURE);
  189.     }
  190.     exit(EXIT_SUCCESS);
  191.  
  192.     /* gcc -Wall pacifier */
  193.     for ( ; ; )
  194.         ;
  195. }
  196.  
  197. static void
  198. hunt(name, lot, hit)
  199. char *    name;
  200. time_t    lot;
  201. time_t    hit;
  202. {
  203.     time_t        t;
  204.     struct tm    lotm;
  205.     struct tm    tm;
  206.     static char    loab[MAX_STRING_LENGTH];
  207.  
  208.     lotm = *localtime(&lot);
  209.     (void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
  210.     while ((hit - lot) >= 2) {
  211.         t = lot / 2 + hit / 2;
  212.         if (t <= lot)
  213.             ++t;
  214.         else if (t >= hit)
  215.             --t;
  216.         tm = *localtime(&t);
  217.         if (delta(&tm, &lotm) == (t - lot) &&
  218.             tm.tm_isdst == lotm.tm_isdst &&
  219.             strcmp(abbr(&tm), loab) == 0) {
  220.                 lot = t;
  221.                 lotm = tm;
  222.         } else    hit = t;
  223.     }
  224.     show(name, lot, TRUE);
  225.     show(name, hit, TRUE);
  226. }
  227.  
  228. static long
  229. delta(newp, oldp)
  230. struct tm *    newp;
  231. struct tm *    oldp;
  232. {
  233.     long    result;
  234.  
  235.     result = newp->tm_hour - oldp->tm_hour;
  236.     if (result < 0)
  237.         result += HOURSPERDAY;
  238.     result *= SECSPERHOUR;
  239.     result += (newp->tm_min - oldp->tm_min) * SECSPERMIN;
  240.     return result + newp->tm_sec - oldp->tm_sec;
  241. }
  242.  
  243. static void
  244. show(zone, t, v)
  245. char *    zone;
  246. time_t    t;
  247. int    v;
  248. {
  249.     struct tm *        tmp;
  250.     extern struct tm *    localtime();
  251.  
  252.     (void) printf("%-*s  ", longest, zone);
  253.     if (v)
  254.         (void) printf("%.24s GMT = ", asctime(gmtime(&t)));
  255.     tmp = localtime(&t);
  256.     (void) printf("%.24s", asctime(tmp));
  257.     if (*abbr(tmp) != '\0')
  258.         (void) printf(" %s", abbr(tmp));
  259.     if (v) {
  260.         (void) printf(" isdst=%d", tmp->tm_isdst);
  261. #ifdef TM_GMTOFF
  262.         (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
  263. #endif /* defined TM_GMTOFF */
  264.     }
  265.     (void) printf("\n");
  266. }
  267.  
  268. static char *
  269. abbr(tmp)
  270. struct tm *    tmp;
  271. {
  272.     register char *    result;
  273.     static char    nada[1];
  274.  
  275.     if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
  276.         return nada;
  277.     result = tzname[tmp->tm_isdst];
  278.     return (result == NULL) ? nada : result;
  279. }
  280.