summaryrefslogtreecommitdiff
path: root/reference/C/PROBLEMS/yesterday.c
blob: 22095f23207e093a3b88c31f39d726e8dc0ca4a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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 <time.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#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);
   }