/* epoch2time.c - Convert seconds since 1/1/1970 to a date/timestamp
  
   December, 1998 - Dennis E. Lovelady

   This program will convert the epoch time (expressed as an integer) to
   a date/timestamp in any format.

   Usage: epoch2time [seconds] [format]
            format:  Same format as used by strftime (man strftime)
                     (Default format is like Tue Sun Dec 06 09:50:11 1998)

   EXAMPLES:
   To convert epoch time to the format and timestamp above:
        epoch2time 912955811

   The following will print the above date in the format of "Sun, Dec 06"
        epoch2time 912955811 "%a, %b %d"
*/

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

char *PgmName ;


void Usage(void)
    {
    fprintf(stderr,
         "\nUsage:\t%s epoch_time [format_string]\n",
         PgmName) ;
    fprintf(stderr,
         "epoch_time: number of seconds since 00:00:00 UTC 1/1/1970") ;
    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[])
    {
    time_t CurrTime ;
    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: CurrTime = atoi(argv[1]) ;
                if (strcmp(argv[1], "-help") == 0)
                    {
                    Usage() ;
                    exit(0) ;
                    }
                break ;
        default:
                Usage() ;
                exit(10) ;
                break ;
        }

    memcpy(&TestTime, localtime(&CurrTime), sizeof(TestTime)) ;
    strftime(buff, sizeof(buff), fmt, &TestTime) ;

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