home *** CD-ROM | disk | FTP | other *** search
/ ftp.freefriends.org / ftp.freefriends.org.tar / ftp.freefriends.org / arnold / Source / strftime-8.0.shar.gz / strftime-8.0.shar / date.c < prev    next >
C/C++ Source or Header  |  2001-07-04  |  1KB  |  71 lines

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