#include /* Standard I/O routines */ #include /* Standard Library routines */ #include /* Standard UNIX routines */ #include /* Time processing routines */ /* * Miscellaneous #defines to satisfy lint */ #define printf ( void )printf #define scanf ( void )scanf #define fprintf ( void )fprintf #define fflush ( void )fflush /* * Useful constants for time processing routines */ #define TIME_ERR ( time_t )-1 #define NOW ( time_t * )0 /* * Hmmm ... */ #define YEAR( tmp ) ( (tmp)->tm_year + 1900 ) #define MONTH( tmp ) ( (tmp)->tm_mon ) #define WEEK_DAY( tmp ) ( (tmp)->tm_wday ) #define MONTH_DAY( tmp ) ( (tmp)->tm_mday ) #define DAY_NAME_STR( tmp ) ( WeekDays[ WEEK_DAY(tmp) ] ) #define MONTH_NAME_STR( tmp ) ( Month[ MONTH(tmp) ].MonthName ) #define DAYS_IN_MONTH( tmp ) ( Month[ MONTH(tmp) ].MonthDays ) /* * These are ... The Days of the Week */ const char *WeekDays[ ] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; /* * An array of structures that contains the * name of and number of days in each month */ struct { unsigned int MonthDays; const char *MonthName; } Month[ ] = { { 31, "January" }, { 28, "February" }, { 31, "March" }, { 30, "April" }, { 31, "May" }, { 30, "June" }, { 31, "July" }, { 31, "August" }, { 30, "September" }, { 31, "October" }, { 30, "November" }, { 31, "December" } }; /* * Main - * Gets relevant calendar information (either user-specified * or from current time and date) and prints it in human * readable form. * * Parameters - * None * * Return value - * Returns final program status * * Error conditions detected - * Several program specific failure modes (individually documented) */ int main( void ) { time_t Date; struct tm *tmDate; #ifdef DEBUG /* * Set up a structure for user-entered info */ static struct tm tmInfo; printf( "Enter the date in numerical form [M D Y]: " ); fflush( stdout ); /* * scanf(3s) is cheesy, but will work for our purposes */ scanf( "%d", &tmInfo.tm_mon ); /* read month */ scanf( "%d", &tmInfo.tm_mday ); /* read day */ scanf( "%d", &tmInfo.tm_year ); /* read year */ /* * User enters month as 1-12, but mktime(3c) expects 0-11 */ tmInfo.tm_mon--; /* * mktime(3c) expects year offset from 1900 */ tmInfo.tm_year -= 1900; /* * Convert user-entered info to UNIX internal time */ Date = mktime( &tmInfo ); #else /* * Convert current time to UNIX internal time */ Date = time( NOW ); #endif /* * Check to see that conversion succeeded - if not, * then we print failure status and exit the program */ if( Date == TIME_ERR ) { fprintf( stderr, "No information is available for that date\n" ); return EXIT_FAILURE; } /* * Extract UNIX internal time so that we can display it */ tmDate = localtime( &Date ); printf( "Today is %s, %3.3s %d, %d\n", DAY_NAME_STR( tmDate ), MONTH_NAME_STR( tmDate ), MONTH_DAY( tmDate ), YEAR( tmDate ) ); #ifdef DEBUG printf( "There are %d days this month\n", DAYS_IN_MONTH( tmDate ) ); #endif /* * Always return a value from main() */ return EXIT_SUCCESS; }