home *** CD-ROM | disk | FTP | other *** search
/ nisttime.carsoncity.k12.mi.us / nisttime.carsoncity.k12.mi.us.tar / nisttime.carsoncity.k12.mi.us / pub / acts / parset.c < prev    next >
C/C++ Source or Header  |  1997-12-08  |  5KB  |  201 lines

  1. void parset(buf)
  2. char buf[280];
  3. {
  4. #include "nistime.h"
  5. #include <stdio.h>
  6. #ifdef IBMPC
  7. #include <dos.h>
  8. #endif
  9. char c;
  10. int j,yr,mo,day,hr,min,sec,is,dst;
  11. #ifdef SUN
  12. #include <sys/time.h>
  13. #endif
  14. #ifdef IBMPC
  15. extern int utcdif;           /* local time - utc in hours */
  16. extern int dsflag;           /* daylight saving time? 1=yes, 0=no */
  17. extern int atflag;           /* AT-type machine? 1=yes, 0=no */
  18. int lday[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; /*last day of month*/
  19. char cnvbcd();
  20. char centc,yrc,moc,dayc,hrc,minc,secc;
  21. int dsdone = 0;
  22. #endif
  23. #ifdef SUN
  24. LONG mjd;
  25. LONG mjd0 = 40587;     /* mjd of 1/1/70 -- origin of SUN time */
  26. struct timeval tvv,*tp;
  27. extern int hs;             /* 1 if dialing at 1200, 0 if at 300   */
  28. LONG cvt2jd();         /* computes MJD from yr-mo-dy          */
  29. #endif
  30. /*
  31.     this subroutine receives a time string in character
  32.     array buf.  it is parsed and used to set the system
  33.     clock.  the origin of the parser is the - character between
  34.     the year and the month so that leading stuff will simply be
  35.     ignored.
  36.  
  37.     this subroutine uses global variables dsflag to check for
  38.     daylight savings time
  39.  
  40.     for the IBMPC version, the subroutine converts to local
  41.     time if requested by the configuration file parameters
  42.     (including a conversion to daylight savings time if needed.
  43.     the SUN version sets the time directly in UTC by converting
  44.     the received time to seconds since 1/1/70.  to simplify
  45.     this conversion, the mjd, rather than the year-month-day
  46.     is used.
  47. */
  48.     for(j=0; (buf[j] != 0) && (buf[j] != '-') ; j++) ;
  49.     sscanf(&buf[j-2],"%2d-%2d-%2d %2d:%2d:%2d %d",&yr,&mo,&day,
  50.     &hr,&min,&sec,&dst);
  51.     if(yr < 80) yr +=100;    /*number of years since 1900*/
  52. #ifdef SUN
  53. /*
  54.     transmission at 1200 contains MJD and it can be read directly
  55.     from NIST.  transmission at 300 does not contain MJD and it must
  56.     be computed from the yr-mo-day values
  57. */
  58.     if(hs==1) sscanf(&buf[j-8],"%5ld",&mjd);
  59.     else      mjd=cvt2jd(yr,mo,day);
  60. /*
  61.     convert received day number to seconds since 1/1/70 and
  62.     add in hour minute and second.
  63. */
  64.     tp= &tvv;
  65.     mjd= 86400*(mjd - mjd0);
  66.     mjd= mjd + 3600*hr + 60*min + sec;
  67.     tp->tv_sec=mjd;
  68.     tp->tv_usec=0;
  69.     settimeofday(tp,0);
  70. #endif
  71. #ifdef IBMPC
  72. /*      
  73.     if IBMPC version is being generated, we must now convert
  74.     to local time (including daylight savings time as needed
  75. */
  76. /*
  77.     make standard-time portion of dst flag contiguous
  78. */
  79.     if(dst == 0) dst = 100;
  80. /*
  81.     convert from utc to local time. note that although minute
  82.     and second are already correct, there is no BIOS call to
  83.     set just those parameters so we have to wait until the hour
  84.     is corrected and then set them all at once.
  85.  
  86.     note that daylight savings time flag must also be updated
  87.     if conversion to local time changes the day
  88.  
  89.     **      following correction is for version of 12 May 88:
  90.     **      this correction compiled for final testing 31 may
  91.     daylight savings time correction is done in two parts:
  92.  
  93.     if we are solidly in daylight savings time, then do correction
  94.     immediately since it may change the day, and therefore it must
  95.     be done before the check for hour/day overflow.
  96.  
  97.     if this is a transition day, then correction cannot be done now
  98.     since the hour/day overflow may change the daylight savings flag
  99.     dst thereby changing whether or not the correction is needed.
  100.     thus correction must be postponed.
  101.  
  102.     parameter dsdone makes sure that the correction is not done twice
  103.     if we are  moved into a transition day by the hour/day overflow */
  104.     if( (yr & 3) == 0 ) lday[2]=29;  /* 29 days for Feb in leap year */
  105.     hr += utcdif;             /* convert hour to local standard time */
  106. /*
  107.     if daylight saving time is selected and it is not a
  108.     transition day, then add 1 hour and set flag to
  109.     show conversion is completed
  110. */
  111.     if( (dsflag !=0) && (dst <= 50) && (dst > 1) )
  112.       {
  113.       hr++;
  114.       dsdone=1;
  115.     }
  116.     if(hr < 0)
  117.       {
  118.       hr += 24;
  119.       day--;
  120.       dst++;         /* update daylight savings flag for change of day*/
  121.       if(day < 1)
  122.         {
  123.         mo--;
  124.         if(mo < 1)
  125.           {
  126.           mo=12;
  127.           yr--;
  128.         }
  129.         day=lday[mo];
  130.       }
  131.     }
  132.     if(hr > 23)
  133.       {
  134.       hr -= 24;
  135.       day++;
  136.       dst--;        /* update daylight saving flag for change of day */
  137.       if(day > lday[mo])
  138.        {
  139.        day=1;
  140.        mo++;
  141.        if(mo > 12)
  142.           {
  143.           mo=1;
  144.           yr++;
  145.        }
  146.       }
  147.     }
  148. /*
  149.     now finish up daylight savings time if enabled
  150.     only thing left to do is to deal with the transition days
  151.     flag dsdone makes sure that transition day is not done twice
  152. */
  153.     if( (dsflag != 0) && (dsdone == 0) )
  154.        {
  155.        if( (dst == 51) && (hr >= 2) ) hr++;
  156.        if( (dst ==  1) && (hr <  2) ) hr++;
  157.     }
  158.     _AH=0x2d;
  159.     _CH=hr;
  160.     _DH=sec;
  161.     _CL=min;
  162.     _DL=10;       /* approximate machine-dependent latency correction*/
  163.     geninterrupt(0x21);
  164.     yr += 1900;       /* add 1900 to number of years since 1900 above*/
  165.     _AH=0x2b;
  166.     _CX=yr;
  167.     _DH=mo;
  168.     _DL=day;
  169.     geninterrupt(0x21);
  170.     printf("\n Computer clock set.");
  171. /*
  172.     code to set AT CMOS clock. note that CMOS clock requires
  173.     time to be converted to BCD digits, packed two to a byte. this
  174.     is done using routine cnvbcd.
  175. */
  176.     if(atflag != 0)
  177.     {
  178.     centc=cnvbcd(yr/100);
  179.     yrc=cnvbcd(yr%100);
  180.     moc=cnvbcd(mo);
  181.     dayc=cnvbcd(day);
  182.     hrc=cnvbcd(hr);
  183.     minc=cnvbcd(min);
  184.     secc=cnvbcd(sec);
  185.     _AH=3;
  186.     _CH=hrc;
  187.     _CL=minc;
  188.     _DH=secc;
  189.     _DL=dsflag;             /* copy daylight savings to CMOS clock */
  190.     geninterrupt(0x1a);
  191.     _AH=5;
  192.     _CH=centc;
  193.     _CL=yrc;
  194.     _DH=moc;
  195.     _DL=dayc;
  196.     geninterrupt(0x1a);
  197.     printf("\n CMOS clock set.");
  198.     }
  199. #endif
  200. }
  201.