/***************************************************************************/
/*                                                                         */
/*     Program:  DST (Daylight Savings Time)                               */
/*  Programmer:  George Kerber                                             */
/*     Written:  05/29/89                                                  */
/*     Purpose:  Adjusts system clock for Daylight Savings Time.           */
/*                                                                         */
/***************************************************************************/

#include <stdio.h>
#include <dos.h>
#define WRITTEN "05/29/89 - 07/08/89"
#define VERSION "v1.06"
int year, dstflag=0 ;
unsigned char clock[8];
char zone[10] = "";

main(argc,argv)
int argc;
char *argv[];

{
int i, startyear, sday, eday, leapyear= 0 ;

if(argc > 2) {
  fprintf(stderr,"\n\07  ERROR:  [33mInvalid Option Count.[0m\07\n\n");
  exit(5);
  }

if(strcmp(argv[1],"?") == 0) {
   printf("\x0c\n  [33m[1mGeorge Kerber %18s %25s[0m\n\n",
           VERSION,WRITTEN);
   printf("  The \"%s\" utility will read the system clock and determine if\n",
             argv[0]);
   printf("  Daylight Savings Time is in effect.  If so, the system clock will\n");
   printf("  be incremented by one hour.\n\n");
   printf("  Your battery clock should always be set for [33m[1mSTANDARD TIME[0m in your area.\n");
   printf("                            [33m[1m~~~~~~[0m\n");
   printf("  Add \"%s [33m[opt][0m\" to the beginning of your startup-sequence after the\n",
             argv[0]);
   printf("  battery clock has been read & env: has been assigned.\n\n");
   printf("  Valid Options:  %s [33me[0m  --  EST/EDT   Eastern Time Zone.\n",argv[0]);
   printf("                  %s [33mc[0m  --  CST/CDT   Central Time Zone.\n",argv[0]);
   printf("                  %s [33mm[0m  --  MST/MDT  Mountain Time Zone.\n",argv[0]);
   printf("                  %s [33mp[0m  --  PST/PDT   Pacific Time Zone.\n",argv[0]);
   printf("                  %s [33md[0m  --  STD/DST   Generic Time Zone.\n\n",argv[0]);
   printf("                  %s [33mf[0m  --  Force evaluation.\n\n",argv[0]);
   printf("  Environmental variable [33mTIMEZONE[0m will be set to the current timezone.\n\n");
   exit(0);
   }

if(argc == 2) {
   switch(argv[1][0]) {
      case 'e': break;
      case 'c': break;
      case 'm': break;
      case 'p': break;
      case 'f': break;
      case 'd': break;
       default: fprintf(stderr,"\n\07  ERROR:  [33mInvalid Option.[0m\07\n\n");
                exit(5);
      }
   }

/* Get battery clock and calculate Daylight Savings Time start date       */

getclk(clock); 

startyear = 1980; sday = 6; eday = 26;            /*  Initialize for 1980 */
for( i = clock[1], leapyear = 0 ; i > 0 ; i--) {
     leapyear = 0;
     startyear++ ;
     if((startyear % 4) == 0) leapyear = 1 ;
     sday-- ;
     if(sday == 0) sday = 7;
     sday -= leapyear;
     if(sday == 0 && leapyear == 1) sday = 7;
     }                                              /*  Ending for brace */

/* Calculate Daylight Savings Time end date using start date              */

if((sday + 20) < 25) 
   eday = sday + 27;
   else
   eday = sday + 20;

/* Determine if it's daylight savings time NOW                            */

if((clock[2] > 4 && clock[2] < 10) ||
    (clock[2] == 4 && clock[3] >= sday && clock[4] >= 2) ||
    (clock[2] == 10 && clock[3] <= eday && clock[4] < 2) ||
    (clock[2] == 4 && clock[3] > sday) ||
    (clock[2] == 10 && clock[3] < eday)) {
    clock[4]++;
    dstflag = 1;
    }

    if(getenv("TIMEZONE") == 0 || stricmp(argv[1],"f") == 0 && dstflag == 1) {
       chgclk(clock);
       }

if(getenv("TIMEZONE") == 0) {
    if(putenv("TIMEZONE=comp")) {
       printf("NOTE:  You must be using Workbench 1.3 or higher and have the\n");
       printf("       ENV: directory already created & assigned.\n\n");
       exit(5);
       }
    }
 
switch(argv[1][0]) {
   case 'e':  strcpy(zone,"Eastern ");
              if(dstflag == 1) putenv("TIMEZONE=EDT");
                          else putenv("TIMEZONE=EST"); 
              break ;
   case 'c':  strcpy(zone,"Central ");
              if(dstflag == 1) putenv("TIMEZONE=CDT");
                          else putenv("TIMEZONE=CST");
              break;
   case 'm':  strcpy(zone,"Mountain ");
              if(dstflag == 1) putenv("TIMEZONE=MDT");
                          else putenv("TIMEZONE=MST");
              break;
   case 'p':  strcpy(zone,"Pacific ");
              if(dstflag == 1) putenv("TIMEZONE=PDT");
                          else putenv("TIMEZONE=PST");
              break;

   case 'd':  if(dstflag == 1) putenv("TIMEZONE=DST");
                          else putenv("TIMEZONE=STD");
              break;

    default:  strcpy(zone,"");
              if(strcmp(getenv("TIMEZONE"),"compTIMEZONE") == 0) {
                 if(dstflag == 1) {
                    putenv("TIMEZONE=DST");
                    }
                    else {
                    putenv("TIMEZONE=STD");
                    }
                 }
              break;
   }

/* Display the current time zone if valid timezone options were supplied.   */

if(argc > 1 && argv[1][0] != 'f' && argv[1][0] != 'd') {
   if(dstflag == 1) {
      printf("[1m%s[33mDaylight[0m[1m Time.[0m\n",zone);
      }
      else {
      printf("[1m%s[33mStandard[0m[1m Time.[0m\n",zone);
      }
}

}     /* Ending brace for Main()                                           */
