home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / STRFTIME.C < prev    next >
C/C++ Source or Header  |  1991-09-23  |  10KB  |  340 lines

  1. /**
  2.  *
  3.  * strftime.c
  4.  *
  5.  * implements the ansi c function strftime()
  6.  *
  7.  * written 6 september 1989 by jim nutt
  8.  * released into the public domain by jim nutt
  9.  *
  10.  * modified 21-Oct-89 by Rob Duff
  11.  *
  12. **/
  13.  
  14. #include <stddef.h>     /* for size_t */
  15. #include <stdarg.h>     /* for va_arg */
  16. #include <time.h>       /* for struct tm */
  17.  
  18. /*
  19. ** The following line should be appended to TIME.H.
  20. ** Also copy size_t define from STRING.H.
  21. */
  22.  
  23. size_t strftime(char *s, size_t maxs, const char *f, const struct tm *t);
  24.  
  25. static char *aday[] = {
  26.     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  27. };
  28.  
  29. static char *day[] = {
  30.     "Sunday", "Monday", "Tuesday", "Wednesday",
  31.     "Thursday", "Friday", "Saturday"
  32. };
  33.  
  34. static char *amonth[] = {
  35.     "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  36.     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  37. };
  38.  
  39. static char *month[] = {
  40.     "January", "February", "March", "April", "May", "June",
  41.     "July", "August", "September", "October", "November", "December"
  42. };
  43.  
  44. extern char *tzname[2];
  45. static char buf[26];
  46.  
  47. static void strfmt(char *str, const char *fmt, ...);
  48.  
  49. /**
  50.  *
  51.  * size_t strftime(char *str,
  52.  *                 size_t maxs,
  53.  *                 const char *fmt,
  54.  *                 const struct tm *t)
  55.  *
  56.  *      this functions acts much like a sprintf for time/date output.
  57.  *      given a pointer to an output buffer, a format string and a
  58.  *      time, it copies the time to the output buffer formatted in
  59.  *      accordance with the format string.  the parameters are used
  60.  *      as follows:
  61.  *
  62.  *          str is a pointer to the output buffer, there should
  63.  *          be at least maxs characters available at the address
  64.  *          pointed to by str.
  65.  *
  66.  *          maxs is the maximum number of characters to be copied
  67.  *          into the output buffer, included the '\0' terminator
  68.  *
  69.  *          fmt is the format string.  a percent sign (%) is used
  70.  *          to indicate that the following character is a special
  71.  *          format character.  the following are valid format
  72.  *          characters:
  73.  *
  74.  *              %A      full weekday name (Monday)
  75.  *              %a      abbreviated weekday name (Mon)
  76.  *              %B      full month name (January)
  77.  *              %b      abbreviated month name (Jan)
  78.  *              %c      standard date and time representation
  79.  *              %d      day-of-month (01-31)
  80.  *              %H      hour (24 hour clock) (00-23)
  81.  *              %I      hour (12 hour clock) (01-12)
  82.  *              %j      day-of-year (001-366)
  83.  *              %M      minute (00-59)
  84.  *              %m      month (01-12)
  85.  *              %p      local equivalent of AM or PM
  86.  *              %S      second (00-59)
  87.  *              %U      week-of-year, first day sunday (00-53)
  88.  *              %W      week-of-year, first day monday (00-53)
  89.  *              %w      weekday (0-6, sunday is 0)
  90.  *              %X      standard time representation
  91.  *              %x      standard date representation
  92.  *              %Y      year with century
  93.  *              %y      year without century (00-99)
  94.  *              %Z      timezone name
  95.  *              %%      percent sign
  96.  *
  97.  *      the standard date string is equivalent to:
  98.  *
  99.  *          %a %b %d %Y
  100.  *
  101.  *      the standard time string is equivalent to:
  102.  *
  103.  *          %H:%M:%S
  104.  *
  105.  *      the standard date and time string is equivalent to:
  106.  *
  107.  *          %a %b %d %H:%M:%S %Y
  108.  *
  109.  *      strftime returns the number of characters placed in the
  110.  *      buffer, not including the terminating \0, or zero if more
  111.  *      than maxs characters were produced.
  112.  *
  113. **/
  114.  
  115. size_t strftime(char *s, size_t maxs, const char *f, const struct tm *t)
  116. {
  117.       int w;
  118.       char *p, *q, *r;
  119.  
  120.       p = s;
  121.       q = s + maxs - 1;
  122.       while ((*f != '\0'))
  123.       {
  124.             if (*f++ == '%')
  125.             {
  126.                   r = buf;
  127.                   switch (*f++)
  128.                   {
  129.                   case '%' :
  130.                         r = "%";
  131.                         break;
  132.  
  133.                   case 'a' :
  134.                         r = aday[t->tm_wday];
  135.                         break;
  136.  
  137.                   case 'A' :
  138.                         r = day[t->tm_wday];
  139.                         break;
  140.  
  141.                   case 'b' :
  142.                         r = amonth[t->tm_mon];
  143.                         break;
  144.  
  145.                   case 'B' :
  146.                         r = month[t->tm_mon];
  147.                         break;
  148.  
  149.                   case 'c' :
  150.                         strfmt(r, "%0 %0 %2 %2:%2:%2 %4",
  151.                               aday[t->tm_wday], amonth[t->tm_mon],
  152.                               t->tm_mday,t->tm_hour, t->tm_min,
  153.                               t->tm_sec, t->tm_year+1900);
  154.                         break;
  155.  
  156.                   case 'd' :
  157.                         strfmt(r,"%2",t->tm_mday);
  158.                         break;
  159.  
  160.                   case 'H' :
  161.                         strfmt(r,"%2",t->tm_hour);
  162.                         break;
  163.  
  164.                   case 'I' :
  165.                         strfmt(r,"%2",(t->tm_hour%12)?t->tm_hour%12:12);
  166.                         break;
  167.  
  168.                   case 'j' :
  169.                         strfmt(r,"%3",t->tm_yday+1);
  170.                         break;
  171.  
  172.                   case 'm' :
  173.                         strfmt(r,"%2",t->tm_mon+1);
  174.                         break;
  175.  
  176.                   case 'M' :
  177.                         strfmt(r,"%2",t->tm_min);
  178.                         break;
  179.  
  180.                   case 'p' :
  181.                         r = (t->tm_hour>11)?"PM":"AM";
  182.                         break;
  183.  
  184.                   case 'S' :
  185.                         strfmt(r,"%2",t->tm_sec);
  186.                         break;
  187.  
  188.                   case 'U' :
  189.                         w = t->tm_yday/7;
  190.                         if (t->tm_yday%7 > t->tm_wday)
  191.                               w++;
  192.                         strfmt(r, "%2", w);
  193.                         break;
  194.  
  195.                   case 'W' :
  196.                         w = t->tm_yday/7;
  197.                         if (t->tm_yday%7 > (t->tm_wday+6)%7)
  198.                               w++;
  199.                         strfmt(r, "%2", w);
  200.                         break;
  201.  
  202.                   case 'w' :
  203.                         strfmt(r,"%1",t->tm_wday);
  204.                         break;
  205.  
  206.                   case 'x' :
  207.                         strfmt(r, "%3s %3s %2 %4", aday[t->tm_wday],
  208.                               amonth[t->tm_mon], t->tm_mday, t->tm_year+1900);
  209.                         break;
  210.  
  211.                   case 'X' :
  212.                         strfmt(r, "%2:%2:%2", t->tm_hour,
  213.                               t->tm_min, t->tm_sec);
  214.                         break;
  215.  
  216.                   case 'y' :
  217.                         strfmt(r,"%2",t->tm_year%100);
  218.                         break;
  219.  
  220.                   case 'Y' :
  221.                         strfmt(r,"%4",t->tm_year+1900);
  222.                         break;
  223.  
  224.                   case 'Z' :
  225.                         r = (t->tm_isdst && tzname[1][0])?tzname[1]:tzname[0];
  226.                         break;
  227.  
  228.                   default:
  229.                         buf[0] = '%';     /* reconstruct the format */
  230.                         buf[1] = f[-1];
  231.                         buf[2] = '\0';
  232.                         if (buf[1] == 0)
  233.                               f--;        /* back up if at end of string */
  234.                   }
  235.                   while (*r)
  236.                   {
  237.                         if (p == q)
  238.                         {
  239.                               *q = '\0';
  240.                               return 0;
  241.                         }
  242.                         *p++ = *r++;
  243.                   }
  244.             }
  245.             else
  246.             {
  247.                   if (p == q)
  248.                   {
  249.                         *q = '\0';
  250.                         return 0;
  251.                   }
  252.                   *p++ = f[-1];
  253.             }
  254.       }
  255.       *p = '\0';
  256.       return p - s;
  257. }
  258.  
  259. /*
  260.  *  stdarg.h
  261.  *
  262. typedef void *va_list;
  263. #define va_start(vp,v) (vp=((char*)&v)+sizeof(v))
  264. #define va_arg(vp,t) (*((t*)(vp))++)
  265. #define va_end(vp)
  266.  *
  267.  */
  268.  
  269. static int pow[5] = { 1, 10, 100, 1000, 10000 };
  270.  
  271. /**
  272.  * static void strfmt(char *str, char *fmt);
  273.  *
  274.  * simple sprintf for strftime
  275.  *
  276.  * each format descriptor is of the form %n
  277.  * where n goes from zero to four
  278.  *
  279.  * 0    -- string %s
  280.  * 1..4 -- int %?.?d
  281.  *
  282. **/
  283.  
  284. static void strfmt(char *str, const char *fmt, ...)
  285. {
  286.       int ival, ilen;
  287.       char *sval;
  288.       va_list vp;
  289.  
  290.       va_start(vp, fmt);
  291.       while (*fmt)
  292.       {
  293.             if (*fmt++ == '%')
  294.             {
  295.                   ilen = *fmt++ - '0';
  296.                   if (ilen == 0)                /* zero means string arg */
  297.                   {
  298.                         sval = va_arg(vp, char*);
  299.                         while (*sval)
  300.                               *str++ = *sval++;
  301.                   }
  302.                   else                          /* always leading zeros */
  303.                   {
  304.                         ival = va_arg(vp, int);
  305.                         while (ilen)
  306.                         {
  307.                               ival %= pow[ilen--];
  308.                               *str++ = '0' + ival / pow[ilen];
  309.                         }
  310.                   }
  311.             }
  312.             else  *str++ = fmt[-1];
  313.       }
  314.       *str = '\0';
  315.       va_end(vp);
  316. }
  317.  
  318. #ifdef TEST
  319.  
  320. #include <stdio.h>      /* for printf */
  321. #include <time.h>       /* for strftime */
  322.  
  323. char test[80];
  324.  
  325. int main(int argc, char *argv[])
  326. {
  327.       int len;
  328.       char *fmt;
  329.       time_t now;
  330.  
  331.       time(&now);
  332.  
  333.       fmt = (argc == 1) ? "%I:%M %p\n%c\n" : argv[1];
  334.       len = strftime(test,sizeof test, fmt, localtime(&now));
  335.       printf("%d: %s\n", len, test);
  336.       return !len;
  337. }
  338.  
  339. #endif /* TEST */
  340.