home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Amiga / other_strftime.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-25  |  5.9 KB  |  261 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 are permitted
  6.  * provided that: (1) source distributions retain this entire copyright
  7.  * notice and comment, and (2) distributions including binaries display
  8.  * the following acknowledgement:  ``This product includes software
  9.  * developed by the University of California, Berkeley and its contributors''
  10.  * in the documentation or other materials provided with the distribution
  11.  * and in all advertising materials mentioning features or use of this
  12.  * software. Neither the name of the University nor the names of its
  13.  * contributors may be used to endorse or promote products derived
  14.  * from this software without specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "@(#)strftime.c    5.8 (Berkeley) 6/1/90";
  22. #endif /* LIBC_SCCS and not lint */
  23.  
  24. /*
  25.  * NOTE: I used the older version of this file, since the 4.3bsd-net2
  26.  *     version uses mktime() from ctime.c, and ctime.c is badly suited
  27.  *     for inclusion in a shared library (too much static stuff...)
  28.  */
  29.  
  30.  
  31. #define TM_YEAR_BASE 1900
  32.  
  33. #include <sys/types.h>
  34. #include <sys/time.h>
  35. #include <time.h>
  36. #include <string.h>
  37.  
  38. static char *afmt[] = {
  39.     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
  40. };
  41. static char *Afmt[] = {
  42.     "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
  43.     "Saturday",
  44. };
  45. static char *bfmt[] = {
  46.     "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  47.     "Oct", "Nov", "Dec",
  48. };
  49. static char *Bfmt[] = {
  50.     "January", "February", "March", "April", "May", "June", "July",
  51.     "August", "September", "October", "November", "December",
  52. };
  53.  
  54. static size_t _fmt(const char *, const struct tm *);
  55. static int _conv (int, int, char);
  56. static int _add (char *);
  57.  
  58. static struct user
  59. {
  60.     char *u_pt;
  61.     size_t u_gsize;
  62. } u;
  63.  
  64. size_t strftime(char *s, size_t maxsize, const char *format, const struct tm *t)
  65. {
  66.     size_t res = 0;
  67.  
  68.     u.u_pt = s;
  69.     if ((u.u_gsize = maxsize) < 1)
  70.           return 0;
  71.     if (_fmt(format, t)) {
  72.         *u.u_pt = '\0';
  73.         res = maxsize - u.u_gsize;
  74.     }
  75.     return res;
  76. }
  77.  
  78. static size_t _fmt(const char *format, const struct tm *t)
  79. {
  80.     for (; *format; ++format) {
  81.         if (*format == '%')
  82.             switch (*++format) {
  83.             case '\0':
  84.                 --format;
  85.                 break;
  86.             case 'A':
  87.                 if (t->tm_wday < 0 || t->tm_wday > 6)
  88.                     return(0);
  89.                 if (!_add(Afmt[t->tm_wday]))
  90.                     return(0);
  91.                 continue;
  92.             case 'a':
  93.                 if (t->tm_wday < 0 || t->tm_wday > 6)
  94.                     return(0);
  95.                 if (!_add(afmt[t->tm_wday]))
  96.                     return(0);
  97.                 continue;
  98.             case 'B':
  99.                 if (t->tm_mon < 0 || t->tm_mon > 11)
  100.                     return(0);
  101.                 if (!_add(Bfmt[t->tm_mon]))
  102.                     return(0);
  103.                 continue;
  104.             case 'b':
  105.             case 'h':
  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 'C':
  112.                 if (!_fmt("%a %b %e %H:%M:%S %Y", t))
  113.                     return(0);
  114.                 continue;
  115.             case 'c':
  116.                 if (!_fmt("%m/%d/%y %H:%M:%S", t))
  117.                     return(0);
  118.                 continue;
  119.             case 'e':
  120.                 if (!_conv(t->tm_mday, 2, ' '))
  121.                     return(0);
  122.                 continue;
  123.             case 'D':
  124.                 if (!_fmt("%m/%d/%y", t))
  125.                     return(0);
  126.                 continue;
  127.             case 'd':
  128.                 if (!_conv(t->tm_mday, 2, '0'))
  129.                     return(0);
  130.                 continue;
  131.             case 'H':
  132.                 if (!_conv(t->tm_hour, 2, '0'))
  133.                     return(0);
  134.                 continue;
  135.             case 'I':
  136.                 if (!_conv(t->tm_hour % 12 ?
  137.                     t->tm_hour % 12 : 12, 2, '0'))
  138.                     return(0);
  139.                 continue;
  140.             case 'j':
  141.                 if (!_conv(t->tm_yday + 1, 3, '0'))
  142.                     return(0);
  143.                 continue;
  144.             case 'k':
  145.                 if (!_conv(t->tm_hour, 2, ' '))
  146.                     return(0);
  147.                 continue;
  148.             case 'l':
  149.                 if (!_conv(t->tm_hour % 12 ?
  150.                     t->tm_hour % 12 : 12, 2, ' '))
  151.                     return(0);
  152.                 continue;
  153.             case 'M':
  154.                 if (!_conv(t->tm_min, 2, '0'))
  155.                     return(0);
  156.                 continue;
  157.             case 'm':
  158.                 if (!_conv(t->tm_mon + 1, 2, '0'))
  159.                     return(0);
  160.                 continue;
  161.             case 'n':
  162.                 if (!_add("\n"))
  163.                     return(0);
  164.                 continue;
  165.             case 'p':
  166.                 if (!_add(t->tm_hour >= 12 ? "PM" : "AM"))
  167.                     return(0);
  168.                 continue;
  169.             case 'R':
  170.                 if (!_fmt("%H:%M", t))
  171.                     return(0);
  172.                 continue;
  173.             case 'r':
  174.                 if (!_fmt("%I:%M:%S %p", t))
  175.                     return(0);
  176.                 continue;
  177.             case 'S':
  178.                 if (!_conv(t->tm_sec, 2, '0'))
  179.                     return(0);
  180.                 continue;
  181.             case 'T':
  182.             case 'X':
  183.                 if (!_fmt("%H:%M:%S", t))
  184.                     return(0);
  185.                 continue;
  186.             case 't':
  187.                 if (!_add("\t"))
  188.                     return(0);
  189.                 continue;
  190.             case 'U':
  191.                 if (!_conv((t->tm_yday + 7 - t->tm_wday) / 7,
  192.                     2, '0'))
  193.                     return(0);
  194.                 continue;
  195.             case 'W':
  196.                 if (!_conv((t->tm_yday + 7 -
  197.                     (t->tm_wday ? (t->tm_wday - 1) : 6))
  198.                     / 7, 2, '0'))
  199.                     return(0);
  200.                 continue;
  201.             case 'w':
  202.                 if (!_conv(t->tm_wday, 1, '0'))
  203.                     return(0);
  204.                 continue;
  205.             case 'x':
  206.                 if (!_fmt("%m/%d/%y", t))
  207.                     return(0);
  208.                 continue;
  209.             case 'y':
  210.                 if (!_conv((t->tm_year + TM_YEAR_BASE)
  211.                     % 100, 2, '0'))
  212.                     return(0);
  213.                 continue;
  214.             case 'Y':
  215.                 if (!_conv(t->tm_year + TM_YEAR_BASE, 4, '0'))
  216.                     return(0);
  217.                 continue;
  218.             case 'Z':
  219.                 /*** XXX if (!t->tm_zone || !_add(t->tm_zone)) ***/
  220.                 if (!_add(tzname[0]))
  221.                     return(0);
  222.                 continue;
  223.             case '%':
  224.             /*
  225.              * X311J/88-090 (4.12.3.5): if conversion char is
  226.              * undefined, behavior is undefined.  Print out the
  227.              * character itself as printf(3) does.
  228.              */
  229.             default:
  230.                 break;
  231.         }
  232.         if (!u.u_gsize--)
  233.             return(0);
  234.         *u.u_pt++ = *format;
  235.     }
  236.     return(u.u_gsize);
  237. }
  238.  
  239. static int _conv(int n, int digits, char pad)
  240. {
  241.     char buf[10];
  242.     register char *p;
  243.  
  244.     buf[sizeof(buf) - 1] = '\0';
  245.     for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
  246.         *p-- = n % 10 + '0';
  247.     while (p > buf && digits-- > 0)
  248.         *p-- = pad;
  249.     return(_add(++p));
  250. }
  251.  
  252. static int _add(char *str)
  253. {
  254.     for (;; ++u.u_pt, --u.u_gsize) {
  255.         if (!u.u_gsize)
  256.             return(0);
  257.         if (!(*u.u_pt = *str++))
  258.             return(1);
  259.     }
  260. }
  261.