From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/PROBLEMS/yesterday.c | 89 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 reference/C/PROBLEMS/yesterday.c (limited to 'reference/C/PROBLEMS/yesterday.c') diff --git a/reference/C/PROBLEMS/yesterday.c b/reference/C/PROBLEMS/yesterday.c new file mode 100644 index 0000000..22095f2 --- /dev/null +++ b/reference/C/PROBLEMS/yesterday.c @@ -0,0 +1,89 @@ +/************************************************************************ + * + * Purpose: By default, display yesterdays date. + * If a number is given on the command line, it is subtracted + * from todays date and the result printed. + * + * Author: M.J. Leslie. + * + * Date: 10-Jan-95 + * + ************************************************************************/ + +#include +#include +#include +#include + +#define DAYS 1 + /* Validate the command line + * parmeters */ +int check_parm(int argc, char *argv[]); + +/************************************************************************/ + +main(int argc, char *argv[]) + { + + time_t seconds; + const int one_day = 86400; /* Number of seconds in a day */ + int days=DAYS; /* Number of days to subtract. */ + + + /* Checkout the cmd line parms */ + if (check_parm( argc, argv)) + { + /* Data is OK. + * Override the default. */ + days = atoi(argv[1]); + } + /* Get current calendar time. + * This is the number of seconds + * since 1-jan-1970 */ + time(&seconds); + + /* Subtract the required number + * of days. */ + seconds -= one_day*days; + + /* Return the required date. */ + printf("%s", ctime(&seconds)); + + /* All done - time to go home. */ + exit(1); + } + +/************************************************************************ + * + * Validate the data enter on the command line. + * + ************************************************************************/ +int check_parm(int argc, char *argv[]) + { + if (argc > 1 ) /* Q. Have we got parms? */ + { + if ( !strcmp("-h", argv[1])) /* Q. Help requested? */ + { + /* Y. */ + printf("%s will return the date of a passed day.", argv[0]); + puts("\nFor example:"); + printf("\t%s \tReturns yesterdays date.\n", argv[0]); + printf("\t%s 1 \tReturns yesterdays date.\n", argv[0]); + printf("\t%s 365\tReturns the date one year ago.\n\n", argv[0]); + exit(0); + } + /* Do a simple check to validate + * the I/P data - not 100% reliable + */ + if (!isdigit(*argv[1])) + { + /* Data naff. + * End the program. */ + printf("%s: Numeric value required.\n", argv[0]); + printf("\n\tTry: %s -h for some info.\n\n", argv[0]); + exit(0); + } + return(1); + } + return(0); + } -- cgit v1.2.3-54-g00ecf