home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / c / cuj9301.zip / 1101116B < prev    next >
Text File  |  1992-11-03  |  891b  |  42 lines

  1. /* time2.c: Compute a future date */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. main()
  8. {
  9.     time_t start, stop;
  10.     struct tm *now;
  11.     int ndays;
  12.  
  13.     /* Get current date and time */
  14.     time(&start);
  15.     now = localtime(&start);
  16.  
  17.     /* Enter an interval in days */
  18.     fputs("How many days from now? ",stderr);
  19.     if (scanf("%d",&ndays) != 1)
  20.         return EXIT_FAILURE;
  21.     now->tm_mday += ndays;
  22.     if (mktime(now) != -1)
  23.         printf("New date: %s",asctime(now));
  24.     else
  25.         puts("Sorry. Can't encode your date.");
  26.     
  27.     /* Calculate elapsed time */
  28.     time(&stop);
  29.     printf("Elapsed program time in seconds: %f\n",
  30.       difftime(stop,start));
  31.  
  32.     return EXIT_SUCCESS;
  33. }
  34.  
  35. /* Output
  36. How many days from now? 45
  37. New date: Fri Nov 20 12:40:32 1992
  38. Elapsed program time in seconds: 1.000000
  39. */
  40.  
  41. /* End of File */
  42.