home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 1 / GoldFishApril1994_CD2.img / d4xx / d473 / cnewssrc / cnews_src.lzh / misc / getdate.c < prev    next >
C/C++ Source or Header  |  1990-12-22  |  1KB  |  79 lines

  1. /*
  2.  * getdate ascii_time ... - print the time_t of ascii_time(s)
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include <time.h>
  8. #include <sys/types.h>
  9. #include <sys/timeb.h>
  10.  
  11. #define    DAY    (24L*60L*60L)
  12.  
  13. struct timeb ftnow;
  14. int exitstatus = 0;
  15.  
  16. char *progname;
  17.  
  18. extern long atol();
  19. extern char *malloc();
  20. extern struct tm *gmtime();
  21. extern time_t time();
  22.  
  23. extern time_t getdate();
  24.  
  25. /* Forwards. */
  26. extern void process();
  27.  
  28. /*
  29.  - main - parse arguments and handle options
  30.  */
  31. main(argc, argv)
  32. int argc;
  33. char *argv[];
  34. {
  35.     register int c;
  36.     register int errflg = 0;
  37.     extern int optind;
  38.     extern char *optarg;
  39.  
  40.     progname = argv[0];
  41.     ftime(&ftnow);
  42.  
  43.     while ((c = getopt(argc, argv, "")) != EOF)
  44.         switch (c) {
  45.         case '?':
  46.         default:
  47.             errflg++;
  48.             break;
  49.         }
  50.     if (errflg || optind == argc) {
  51.         (void) fprintf(stderr, "Usage: %s ascii_time ...\n", progname);
  52.         exit(2);
  53.     }
  54.  
  55.     for (; optind < argc; optind++)
  56.         process(argv[optind]);
  57.     exit(exitstatus);
  58. }
  59.  
  60. /*
  61.  * process - print time_t of tm
  62.  */
  63. void
  64. process(tm)
  65. char *tm;
  66. {
  67.     time_t it;
  68.  
  69.     if (strcmp(tm, "now") == 0)
  70.         it = time((time_t *)NULL);
  71.     else
  72.         it = getdate(tm, &ftnow);
  73.     if (it < 0) {
  74.         (void) fprintf(stderr, "%s: `%s' not a valid date\n", progname, tm);
  75.         exitstatus = 1;
  76.     } else
  77.         (void) printf("%ld\n", it);
  78. }
  79.