home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / posix-date / date.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-29  |  1.0 KB  |  62 lines

  1. /*
  2.  * date.c
  3.  *
  4.  * Public domain implementation of Posix 1003.2 Draft 11
  5.  * date command.  Lets strftime() do the dirty work.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <sys/types.h>
  10. #include <time.h>
  11.  
  12. extern char *malloc();
  13. extern size_t strftime();
  14. extern int getopt();
  15. extern int optind;
  16.  
  17. int
  18. main(argc, argv)
  19. int argc;
  20. char **argv;
  21. {
  22.     time_t clock;
  23.     struct tm *now;
  24.     int c, size, ret;
  25.     char *defhow = "%a %b %e %H:%M:%S %Z %Y";
  26.     char *howto = defhow;
  27.     char *buf;
  28.  
  29.     while ((c = getopt(argc, argv, "u")) != -1)
  30.         switch (c) {
  31.         case 'u':
  32.             putenv("TZ=GMT0");
  33.             break;
  34.         default:
  35.             fprintf(stderr, "usage: %s [-u] [+format_str]\n",
  36.                 argv[0]);
  37.             exit(1);
  38.         }
  39.  
  40.     time(& clock);
  41.     now = localtime(& clock);
  42.  
  43.     if (optind < argc && argv[optind][0] == '+')
  44.         howto = & argv[optind][1];
  45.  
  46.     size = strlen(howto) * 10;
  47.     if ((buf = malloc(size)) == NULL) {
  48.         perror("not enough memory");
  49.         exit(1);
  50.     }
  51.  
  52.     ret = strftime(buf, size, howto, now);
  53.     if (ret != 0)
  54.         printf("%s\n", buf);
  55.     else {
  56.         fprintf(stderr, "conversion failed\n");
  57.         exit(1);
  58.     }
  59.     
  60.     exit(0);
  61. }
  62.