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

int main()
{
  struct tm input_time;
  char *buffer;
  int option;

  if ((buffer = (char *)malloc(80)) == NULL) {
    fprintf(stderr,"ERROR: Out of memory\n");
    exit(4);
  }

  printf("\nPlease choose an option:\n\n");
  printf("\t1) Current time\n");
  printf("\t2) User-supplied time\n\n");
  do {
    printf("Option by number --> ");
    fgets(buffer,80,stdin);
  } while ((sscanf(buffer,"%d",&option) != 1) || (option < 1) || (option > 2));

  if (option == 1)
    printf("\nThe current time is %ld\n",time(NULL));
  else {
    printf("\nPlease enter the following elements of the time:\n\n");
    do {
      printf("Please enter the number of seconds (0-59)           --> ");
      fgets(buffer,80,stdin);
    } while ((sscanf(buffer,"%d",&(input_time.tm_sec)) != 1) ||
             (input_time.tm_sec < 0) || (input_time.tm_sec > 59));
    do {
      printf("Please enter the number of minutes (0-59)           --> ");
      fgets(buffer,80,stdin);
    } while ((sscanf(buffer,"%d",&(input_time.tm_min)) != 1) ||
             (input_time.tm_min < 0) || (input_time.tm_min > 59));
    do {
      printf("Please enter the number of hours (0-23)             --> ");
      fgets(buffer,80,stdin);
    } while ((sscanf(buffer,"%d",&(input_time.tm_hour)) != 1) ||
             (input_time.tm_hour < 0) || (input_time.tm_hour > 23));
    do {
      printf("Please enter the day of the month (1-31)            --> ");
      fgets(buffer,80,stdin);
    } while ((sscanf(buffer,"%d",&(input_time.tm_mday)) != 1) ||
             (input_time.tm_mday < 1) || (input_time.tm_hour > 31));
    do {
      printf("Please enter the month number (1-12)                --> ");
      fgets(buffer,80,stdin);
    } while ((sscanf(buffer,"%d",&(input_time.tm_mon)) != 1) ||
             (input_time.tm_mon < 1) || (input_time.tm_mon > 12));
    input_time.tm_mon--;  /* Must be 0 to 11 */
    do {
      printf("Please enter the number of years since 1900         --> ");
      fgets(buffer,80,stdin);
    } while ((sscanf(buffer,"%d",&(input_time.tm_year)) != 1) ||
             (input_time.tm_year < 0) || (input_time.tm_year > 199));
    do {
      printf("Please enter 1 for Daylight time, 0 for Solar time  --> ");
      fgets(buffer,80,stdin);
    } while ((sscanf(buffer,"%d",&(input_time.tm_isdst)) != 1) ||
             (input_time.tm_isdst < 0) || (input_time.tm_isdst > 1));
/* Working here.  Must determine the day of the week and the number
   of days since Jan 1. */

/* Experiment */
    input_time.tm_wday = -1;
    input_time.tm_yday = -1;

/* Invoke mktime and print result */

    printf("The supplied time is %ld\n",mktime(&input_time));
  }

  return 0;
}

