/*+JMJ
*      ZOTIME - Print date and time in user's local time zone
*      Copyright 2011 David Meyer <[email protected]>
*
*      TOPS-20 DAYTIME prints only the time in host's local
*      time without zone designation.
*/

#include <stdio.h>
#include <string.h>
#include <time.h>

#define INIFILE "zotime.ini"

char* month[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

char* wkday[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

int main()
{
       FILE *inif;
       char zostr[7], outstr[7];
       int hrset, num, stri, zosign, zohr, zomin;
       time_t timer;
       struct tm *now;

       timer = time(NULL);
       inif = fopen(INIFILE, "r");
       /* What do we do with empty or non-existent inifile? */
       while (fgets(zostr, 7, inif) != NULL)
         {
           hrset = num = stri = zohr = zomin = 0;
           zosign = 1;
           for (stri = 0; stri < strlen(zostr); stri ++)
             {
               switch (zostr[stri])
                 {
                 case '-':
                   if (stri == 0)
                     zosign = -1;
                   else
                     /* error */;
                   break;
                 case '+':
                   if (stri != 0)
                     /* error */;
                   break;
                 case ':':
                   if (hrset)
                     /* error */;
                   else
                     {
                       zohr = num * zosign;
                       hrset = 1;
                     }
                   break;
                 default:
                   if ('0' <= zostr[stri] <= '9')
                     num = num * 10 + (int) (zostr[stri] - '0');
                   else
                     /* error */;
                 }
             }
           if (hrset)
             zomin = num * zosign;
           else
             zohr = num * zosign;
           /* construct TZ string and print date/time */
           timer += (zohr * 3600 + zomin * 60);
           now = gmtime(&timer);
           if (zohr || zomin)
             if (zomin)
               sprintf(outstr, "%+d:%0d", zohr, abs(zomin));
             else
               sprintf(outstr, "%+d", zohr);
           else
             strcpy(outstr, "");
           /*
           printf("%s\n", zostr);
           */
           printf(" %s, %s %d, %04d %02d:%02d:%02d UTC%s\n", wkday[now->tm_wday], month[now->tm_mon], now->tm_mday, now->tm_year + 1900, now->tm_hour, now->tm_min, now->tm_sec, outstr);
         }
       fclose(inif);
       exit(0);
}