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

  1. /* _Isdst function */
  2. #include <stdlib.h>
  3. #include "xtime.h"
  4.  
  5. int _Isdst(const struct tm *t)
  6.     {    /* test whether Daylight Savings Time
  7.             in effect */
  8.     Dstrule *pr;
  9.     static const char *olddst = NULL;
  10.     static Dstrule *rules = NULL;
  11.  
  12.     if (olddst != _Times._Isdst)
  13.         {    /* find current dst_rules */
  14.         if (_Times._Isdst[0] == '\0')
  15.             {    /* look beyond time_zone info */
  16.             int n;
  17.  
  18.             if (_Times._Tzone[0] == '\0')
  19.                 _Times._Tzone = _Getzone();
  20.             _Times._Isdst = _Gettime(_Times._Tzone,
  21.                 3, &n);
  22.             if (_Times._Isdst[0] != '\0')
  23.                 --_Times._Isdst;    /* point to
  24.                     delimiter */
  25.             }
  26.         if ((pr = _Getdst(_Times._Isdst)) == NULL)
  27.             return (-1);
  28.         free(rules);
  29.         rules = pr;
  30.         olddst = _Times._Isdst;
  31.         }
  32.      {    /* check time against rules */
  33.     int ans = 0;
  34.     const int d0 = _Daysto(t->tm_year, 0);
  35.     const int hour = t->tm_hour + 24 * t->tm_yday;
  36.     const int wd0 = (365L * t->tm_year + d0 + WDAY)
  37.             % 7 + 14;
  38.  
  39.     for (pr = rules; pr->wday != (unsigned char)-1; ++pr)
  40.         if (pr->year <= t->tm_year)
  41.             {    /* found early enough year */
  42.             int rday = _Daysto(t->tm_year, pr->mon)
  43.                 - d0 + pr->day;
  44.  
  45.             if (0 < pr->wday)
  46.                 {    /* shift to specific weekday */
  47.                 int wd = (rday + wd0 - pr->wday) % 7;
  48.  
  49.                 rday += wd == 0 ? 0 : 7 - wd;
  50.                 if (pr->wday <= 7)
  51.                     rday -= 7;    /* strictly before */
  52.                 }
  53.             if (hour < rday * 24 + pr->hour)
  54.                 return (ans);
  55.             ans = pr->year == (pr + 1)->year
  56.                 ? !ans : 0;
  57.             }
  58.     return (ans);
  59.      }
  60.     }
  61.  
  62.