/* adddate.c - Generate a date, given a number-of-days
//
// October, 1997 - Dennis E. Lovelady
// July,    2000 - Added capability of compiling for non threadsafe (some compilers lack libs)
// August,  2000 - Squashed bug associated with date change and time zones
//              corrected.  Thanks to Bob Vance for pointing this out.
//
// This program will generate a date, the specified number of days
// from today.  The current date is established, then the passed
// parameter is added to it, to come up with the new date.
//
// Usage: adddate number_of_days [format]
//          format:  Same format as used by strftime (man strftime)
//                   (Default format is like Sun Sept 16 01:03:52 1997)
//
// EXAMPLES:
// To find yesterday's date:
//      adddate -1
//
// To find tomorrow's date:
//      adddate 1
//
// The following will print the date from this day last week, in the
// format of "Tue, May 1"
//      adddate -7 "%a, %b %d"
*/


/* #define THREADSAFE 1 /* Comment this line for non-thread-safe (usually OK) */

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

#define BOOL char
#define SECONDS_PER_DAY 86400

BOOL Weekdays[7], Months[13], Days[31] ;

char *PgmName ;




void Usage(void)
    {
    fprintf(stderr,
         "\nUsage:\t%s number_of_days [format_string]\n",
         PgmName) ;
    fprintf(stderr,
         "number_of_days: Offset from today (-1 = yesterday)\n") ;
    fprintf(stderr,
         "format:  Same format as used by strftime (man strftime)\n") ;
    fprintf(stderr,
         "         (Default format is like Sun Sep 16 01:03:52 1997)\n") ;
    fprintf(stderr,
          "The following will print the yesterday's date, formatted"
          " like \"Tue, May 1\"") ;
    fprintf(stderr,
          "\n\t%s -1 \"%%a, %%b %%d\"\n", PgmName) ;
    fprintf(stderr,
          "You can generate this usage statement with:\n\t%s -help\n\n",
          PgmName) ;
    }





int main(int argc, char *argv[])
    {
    int days;
    time_t CurrTime ;
    unsigned long WorkClock ;
    struct tm TestTime ;
    char *fmt = "%a %b %d %H:%M:%S %Y" ;
    char buff[256] ;

    PgmName = argv[0] ;


    /* Unusual usage of 'switch - note absence of 'break' */
    switch (argc)
        {
        case 3: fmt = argv[2] ;
        case 2: days = atoi(argv[1]) ;
                if (strcmp(argv[1], "-help") == 0)
                    {
                    Usage() ;
                    exit(0) ;
                    }
                break ;
        default:
                Usage() ;
                exit(10) ;
                break ;
        }

    time(&CurrTime) ;
#ifdef THREADSAFE
    localtime_r(&CurrTime, &TestTime) ;
#else
    memcpy(&TestTime, localtime(&CurrTime), sizeof(TestTime));
#endif
    TestTime.tm_mday += days;
    TestTime.tm_isdst = -1;  /* Figure out whether DST applies */
    CurrTime=mktime(&TestTime); 
#ifdef THREADSAFE
    localtime_r(&CurrTime, &TestTime) ;
#else
    memcpy(&TestTime, localtime(&CurrTime),sizeof(TestTime));
#endif
    strftime(buff, sizeof(buff), fmt, &TestTime) ;

    printf("%s\n", buff) ;
    return 0 ;
    }



