home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / STRFTIME.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  10KB  |  338 lines

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