
/*
*---------------------------------------------------------------------
*              Written and Copyright ©1993 by Robert Lang
*---------------------------------------------------------------------
*
* File    : test_time.c
*
* Project : Edge Library
*
* Date    : 14th November 1993
*
*---------------------------------------------------------------------
*
* Description
*
*   Program to test functions in edgetime.c
*
*---------------------------------------------------------------------
*
* Functions
*
*   main()
*
*---------------------------------------------------------------------
*
* History
*
*   Who         When          What
*
*   R.Lang      14-NOV-93     Creation
*
*---------------------------------------------------------------------
*/

/* System Includes */

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

/* Edge Library Includes */

#include <edgetime.h>

/* local function protos */

void usage( void );

/*
*---------------------------------------------------------------------
*
* Function     : main()
*
* Description  : Mainline !
*
* Arguments    : none to show current month,
*                1 argument - month number of the current year
*                2 arguments - month followed by year
*
* Results      : always 0
*
* Global       : none
*
* Comments     : none
*
*---------------------------------------------------------------------
*/
int main( int argc, char *argv[] )
{
  time_t tim;             /* current time in seconds      */
  struct tm *ltim = NULL; /* current time (sensible)      */
  char month;             /* month to work with           */
  int  year;              /* year to work with            */
  char day;               /* day of week for 1st of month */
  char i,j;               /* miscellaneous counters       */

/* get current time in sensible format */

  time( &tim );
  ltim = localtime( &tim );

/* parse arguments */

  switch( argc )
  {
    case 0:
    case 1:
      month = ltim->tm_mon+1;
      year  = ltim->tm_year;
      break;
    case 2:
      month = atoi( argv[1] );
      year  = ltim->tm_year;
      break;
    case 3:
      month = atoi( argv[1] );
      year  = atoi( argv[2] );
      break;
    default:
      usage();
  }

/* convenient: adjust year if less than 100 */

  if( year < 100 )
    year += 1900;

/* get day that first of the month starts on */

  day = timWeekDay( 1, month, year );

  if( day < 0 )
  {
    printf("Error! Code: [%d]\n",day);
    exit(0);
  }

/* output a simple calendar */

  printf("\n Su Mo Tu We Th Fr Sa\n\n ");

  for( i=0,j=1; i<day; i++ )
    printf("   ");

  while( j<=timDays( month, year ) )
  {
    printf("%2d ",j++);
    if( ((++i)%7) == 0 )
      printf("\n ");
  }

  printf("\n");
  if( i%7 )
    printf("\n");

/* only a test program - always return 0 */

  return 0;

} /* main() */

/*
*---------------------------------------------------------------------
*
* Function     : usage()
*
* Description  : Show usage of program and exit.
*
* Arguments    : none
*
* Results      : program exits
*
* Global       : none
*
* Comments     : none
*
*---------------------------------------------------------------------
*/
void usage( void )
{
  printf("\nTime test program - shows a calendar.\n\n");
  printf("usage: test_time [month] [year]\n\n");
  printf("for month (1-12) of year (or current)\n\n");
  printf("(Edge Library ©1993 Robert Lang.)\n\n");
  exit(0);
} /* usage() */
