/* cvtdat.c - Convert a date to any format 

   (c) Dennis Lovelady   http://www.lovelady.com/mailform/
       This program may be freely distributed, but must contain the copyright
       information.

   usage:  cvtdat Mth Day Year [Format]
                  Mth is a value between 1 and 12
                  Day is a value between 1 and 31
                  Year is a 2-digit or 4-digit year
                  Format follows the same usage as the format of command "date"
                  Examples: To obtain the julian day for 23-mar-99:
                                        cvtdat 3 23 99 "%j"
                                 -or-   cvtdat 3 23 1999 "%j"
                            To obtain the weekday for 1-Nov-83:
                                        cvtdat 1 11 83 "%A"
                
*/

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

char *Copyright="(C) 1998, Dennis Lovelady" ;

void Usage(void *pgm)
    {
    fprintf(stderr, "Usage: %s Mth Day Year [Format]\n", pgm) ;
    fprintf(stderr, "Format follows the same format as the date command.\n");
    }

int main(int argc, char *argv[]) 
    {
    time_t  t1;
    struct tm date;
    char *Day[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
    char *fmt = "%a %b %d, %Y" ;
    char buff[256];

    if (argc < 4)
        {
        Usage(argv[0]) ;
        exit(1) ;
        }
    if (argc > 4)
        fmt = argv[4] ;

    time(&t1);
    memcpy(&date, localtime(&t1), sizeof(date)) ;
    date.tm_mon = atoi(argv[1]) - 1;
    date.tm_mday= atoi(argv[2]);
    date.tm_year= atoi(argv[3]);

    if (date.tm_year > 1900)
        date.tm_year -= 1900 ;
    else if (date.tm_year < 50)
        date.tm_year += 100;
    date.tm_isdst = -1;            /* Figure out if DST applies */
    t1 = mktime(&date) ;
    memcpy(&date, localtime(&t1), sizeof(date));

    strftime(buff, sizeof(buff), fmt, &date) ;

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