home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / LIBSRC.ZOO / libsrc / string / strftime.c < prev    next >
C/C++ Source or Header  |  1992-01-31  |  7KB  |  292 lines

  1. /*
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)strftime.c    5.11 (Berkeley) 2/24/91";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #include <sys/types.h>
  39. #include <sys/time.h>
  40. #include <tzfile.h>
  41. #include <string.h>
  42.  
  43. static char *afmt[] = {
  44.     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
  45. };
  46. static char *Afmt[] = {
  47.     "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
  48.     "Saturday",
  49. };
  50. static char *bfmt[] = {
  51.     "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  52.     "Oct", "Nov", "Dec",
  53. };
  54. static char *Bfmt[] = {
  55.     "January", "February", "March", "April", "May", "June", "July",
  56.     "August", "September", "October", "November", "December",
  57. };
  58.  
  59. static size_t gsize;
  60. static char *pt;
  61. static int _add(), _conv(), _secs();
  62.  
  63. size_t
  64. strftime(s, maxsize, format, t)
  65.     char *s;
  66.     size_t maxsize;
  67.     const char *format;
  68.     const struct tm *t;
  69. {
  70.     static size_t _fmt();
  71.  
  72.     pt = s;
  73.     if ((gsize = maxsize) < 1)
  74.         return(0);
  75.     if (_fmt(format, t)) {
  76.         *pt = '\0';
  77.         return(maxsize - gsize);
  78.     }
  79.     return(0);
  80. }
  81.  
  82. static size_t
  83. _fmt(format, t)
  84.     register char *format;
  85.     struct tm *t;
  86. {
  87.     for (; *format; ++format) {
  88.         if (*format == '%')
  89.             switch(*++format) {
  90.             case '\0':
  91.                 --format;
  92.                 break;
  93.             case 'A':
  94.                 if (t->tm_wday < 0 || t->tm_wday > 6)
  95.                     return(0);
  96.                 if (!_add(Afmt[t->tm_wday]))
  97.                     return(0);
  98.                 continue;
  99.             case 'a':
  100.                 if (t->tm_wday < 0 || t->tm_wday > 6)
  101.                     return(0);
  102.                 if (!_add(afmt[t->tm_wday]))
  103.                     return(0);
  104.                 continue;
  105.             case 'B':
  106.                 if (t->tm_mon < 0 || t->tm_mon > 11)
  107.                     return(0);
  108.                 if (!_add(Bfmt[t->tm_mon]))
  109.                     return(0);
  110.                 continue;
  111.             case 'b':
  112.             case 'h':
  113.                 if (t->tm_mon < 0 || t->tm_mon > 11)
  114.                     return(0);
  115.                 if (!_add(bfmt[t->tm_mon]))
  116.                     return(0);
  117.                 continue;
  118.             case 'C':
  119.                 if (!_fmt("%a %b %e %H:%M:%S %Y", t))
  120.                     return(0);
  121.                 continue;
  122.             case 'c':
  123.                 if (!_fmt("%m/%d/%y %H:%M:%S", t))
  124.                     return(0);
  125.                 continue;
  126.             case 'D':
  127.                 if (!_fmt("%m/%d/%y", t))
  128.                     return(0);
  129.                 continue;
  130.             case 'd':
  131.                 if (!_conv(t->tm_mday, 2, '0'))
  132.                     return(0);
  133.                 continue;
  134.             case 'e':
  135.                 if (!_conv(t->tm_mday, 2, ' '))
  136.                     return(0);
  137.                 continue;
  138.             case 'H':
  139.                 if (!_conv(t->tm_hour, 2, '0'))
  140.                     return(0);
  141.                 continue;
  142.             case 'I':
  143.                 if (!_conv(t->tm_hour % 12 ?
  144.                     t->tm_hour % 12 : 12, 2, '0'))
  145.                     return(0);
  146.                 continue;
  147.             case 'j':
  148.                 if (!_conv(t->tm_yday + 1, 3, '0'))
  149.                     return(0);
  150.                 continue;
  151.             case 'k':
  152.                 if (!_conv(t->tm_hour, 2, ' '))
  153.                     return(0);
  154.                 continue;
  155.             case 'l':
  156.                 if (!_conv(t->tm_hour % 12 ?
  157.                     t->tm_hour % 12 : 12, 2, ' '))
  158.                     return(0);
  159.                 continue;
  160.             case 'M':
  161.                 if (!_conv(t->tm_min, 2, '0'))
  162.                     return(0);
  163.                 continue;
  164.             case 'm':
  165.                 if (!_conv(t->tm_mon + 1, 2, '0'))
  166.                     return(0);
  167.                 continue;
  168.             case 'n':
  169.                 if (!_add("\n"))
  170.                     return(0);
  171.                 continue;
  172.             case 'p':
  173.                 if (!_add(t->tm_hour >= 12 ? "PM" : "AM"))
  174.                     return(0);
  175.                 continue;
  176.             case 'R':
  177.                 if (!_fmt("%H:%M", t))
  178.                     return(0);
  179.                 continue;
  180.             case 'r':
  181.                 if (!_fmt("%I:%M:%S %p", t))
  182.                     return(0);
  183.                 continue;
  184.             case 'S':
  185.                 if (!_conv(t->tm_sec, 2, '0'))
  186.                     return(0);
  187.                 continue;
  188.             case 's':
  189.                 if (!_secs(t))
  190.                     return(0);
  191.                 continue;
  192.             case 'T':
  193.             case 'X':
  194.                 if (!_fmt("%H:%M:%S", t))
  195.                     return(0);
  196.                 continue;
  197.             case 't':
  198.                 if (!_add("\t"))
  199.                     return(0);
  200.                 continue;
  201.             case 'U':
  202.                 if (!_conv((t->tm_yday + 7 - t->tm_wday) / 7,
  203.                     2, '0'))
  204.                     return(0);
  205.                 continue;
  206.             case 'W':
  207.                 if (!_conv((t->tm_yday + 7 -
  208.                     (t->tm_wday ? (t->tm_wday - 1) : 6))
  209.                     / 7, 2, '0'))
  210.                     return(0);
  211.                 continue;
  212.             case 'w':
  213.                 if (!_conv(t->tm_wday, 1, '0'))
  214.                     return(0);
  215.                 continue;
  216.             case 'x':
  217.                 if (!_fmt("%m/%d/%y", t))
  218.                     return(0);
  219.                 continue;
  220.             case 'y':
  221.                 if (!_conv((t->tm_year + TM_YEAR_BASE)
  222.                     % 100, 2, '0'))
  223.                     return(0);
  224.                 continue;
  225.             case 'Y':
  226.                 if (!_conv(t->tm_year + TM_YEAR_BASE, 4, '0'))
  227.                     return(0);
  228.                 continue;
  229.             case 'Z':
  230.                 if (!t->tm_zone || !_add(t->tm_zone))
  231.                     return(0);
  232.                 continue;
  233.             case '%':
  234.             /*
  235.              * X311J/88-090 (4.12.3.5): if conversion char is
  236.              * undefined, behavior is undefined.  Print out the
  237.              * character itself as printf(3) does.
  238.              */
  239.             default:
  240.                 break;
  241.         }
  242.         if (!gsize--)
  243.             return(0);
  244.         *pt++ = *format;
  245.     }
  246.     return(gsize);
  247. }
  248.  
  249. static
  250. _secs(t)
  251.     struct tm *t;
  252. {
  253.     static char buf[15];
  254.     register time_t s;
  255.     register char *p;
  256.     struct tm tmp;
  257.  
  258.     /* Make a copy, mktime(3) modifies the tm struct. */
  259.     tmp = *t;
  260.     s = mktime(&tmp);
  261.     for (p = buf + sizeof(buf) - 2; s > 0 && p > buf; s /= 10)
  262.         *p-- = s % 10 + '0';
  263.     return(_add(++p));
  264. }
  265.  
  266. static
  267. _conv(n, digits, pad)
  268.     int n, digits;
  269.     char pad;
  270. {
  271.     static char buf[10];
  272.     register char *p;
  273.  
  274.     for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
  275.         *p-- = n % 10 + '0';
  276.     while (p > buf && digits-- > 0)
  277.         *p-- = pad;
  278.     return(_add(++p));
  279. }
  280.  
  281. static
  282. _add(str)
  283.     register char *str;
  284. {
  285.     for (;; ++pt, --gsize) {
  286.         if (!gsize)
  287.             return(0);
  288.         if (!(*pt = *str++))
  289.             return(1);
  290.     }
  291. }
  292.